定制 yunwuxin/think-throttle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-01-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固