yunwuxin/think-throttle
Composer 安装命令:
composer require yunwuxin/think-throttle
包简介
Throttle is a rate limiter for thinkphp6
README 文档
README
composer require yunwuxin/think-throttle
下面示例均为限制每分钟允许10次访问
使用方式
门面方式
//... use yunwuxin\throttle\facade\RateLimiter; //... $key = 'xxxx'; if(RateLimiter::tooManyAttempts($key, 10)) { //超出频率限制了 throw new \Exception('....'); } RateLimiter::hit($key, 60); //....其他操作
依赖注入方式
use yunwuxin\throttle\RateLimiter; class SomeClass { public function index(Ratelimiter $limiter) { $key = 'xxxx'; if($limiter->tooManyAttempts($key, 10)) { //超出频率限制了 throw new \Exception('....'); } $limiter->hit($key, 60); //....其他操作 } }
中间件方式
use yunwuxin\throttle\middleware\ThrottleRequests; use think\facade\Route; Route::group(function(){ //路由注册 })->middleware(ThrottleRequests::class, 10);
中间件支持3个参数 $maxAttempts, $decayMinutes, $prefix
...->middleware(ThrottleRequests::class, $maxAttempts, $decayMinutes, $prefix);
$maxAttempts: 可访问次数,默认值60
$decayMinutes: 单位时间,默认值1分钟
$prefix: $key的前缀,默认值为空,可以传入一个闭包,返回一个字符串作为$key,该闭包支持依赖注入
统计信息
- 总下载量: 62
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-01-08