基于redis实现接口限流,主要使用Redis的一个模块:Redis-Cell
话不多述直接上代码:
先看下我项目目录:
LeakyBucket实现类
namespace app\common\server;
class LeakyBucket
{
protected $key = null;
protected $max_burst = null;
protected $tokens = null;
protected $seconds = null;
protected $apply = 1;
protected $redis_connect;
/**
* LeakyBucket construct
* @param $key string
* @param $max_burst int 初始桶数量
* @param $tokens int 速率
* @param $seconds int 时间
* @param int $apply 每次漏水数量
*/
public function __construct($key, $max_burst, $tokens, $seconds, $apply = 1)
{
$thi