Skip to content

适配 hyperf 框架的请求频率限流器,基于 Hyperf\Redis\Redis 实现

License

Notifications You must be signed in to change notification settings

kabudin/rate-limit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rate-limit

适配 hyperf 框架的请求频率限流器,基于 Hyperf\Redis\Redis 实现

安装

composer require bud/rate-limit

注解使用

以下代码仅做用法展示

<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Bud\RateLimit\Annotation\RateLimitAnnotation;

#[AutoController(prefix: "test")]
class TestController
{
    /**
     * 20秒内仅允许请求一次
     */
    #[RequestMapping(path: "", methods: "get"),RateLimitAnnotation('test', 20, 1)]
    public function index()
    {
        return [
            'key' => 'rate_limit:App\Controller\IndexController:index:test'
        ];
    }

    /**
     * 相同路径参数一分钟仅允许访问一次
     */
    #[RequestMapping(path: "{id}", methods: "get"),RateLimitAnnotation('test:{id}', 60, 1)]
    public function info(int $id)
    {
        return [
            'key' => "rate_limit:App\Controller\IndexController:info:test:$id"
        ];
    }
}

静态方法使用

    /**
     * @param string $key 限流键
     * @param int $unit_time 单位时间(秒)默认(60)秒
     * @param int $max_number 最大访问次数 默认60次,即一秒一次
    */
    \Zeno\RateLimit\RateLimit::checkLimit(string $key, int $unit_time = 60, int $max_number = 60)

About

适配 hyperf 框架的请求频率限流器,基于 Hyperf\Redis\Redis 实现

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages