php redis-cell限流

1.redis-cell 安装# 设置下载位置
 

mkdir redis-cell

# 在该位置 下载包
wget https://github.com/brandur/redis-cell/releases/download/v0.2.5/redis-cell-v0.2.5-x86_64-unknown-linux-gnu.tar.gz

# 解压
tar -zxvf redis-cell/releases/download/v0.2.5/redis-cell-v0.2.5-x86_64-unknown-linux-gnu.tar.gz

# pwd 复制位置 修改redis.conf文件 加载新模块
vim redis.conf
# :/loadm 就能找到了
loadmodule pwd刚才的地址/libredis_cell.so
# esc :wq
# 重启redis
redis-server redis.conf
# 登陆进来 输入CL.THROTTLE命令 若是存在 说明安装成功
redis-cli -a 密码
CL.THROTTLE

2.如果启动失败提示

10066:C 22 Mar 2021 21:01:45.320 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10066:C 22 Mar 2021 21:01:45.320 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=10066, just started
10066:C 22 Mar 2021 21:01:45.321 # Configuration loaded
10066:M 22 Mar 2021 21:01:45.324 * Increased maximum number of open files to 10032 (it was originally set to 1024).
10066:M 22 Mar 2021 21:01:45.324 * monotonic clock: POSIX clock_gettime
10066:M 22 Mar 2021 21:01:45.327 * Running mode=standalone, port=6379.
10066:M 22 Mar 2021 21:01:45.327 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
10066:M 22 Mar 2021 21:01:45.327 # Server initialized
10066:M 22 Mar 2021 21:01:45.327 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
10066:M 22 Mar 2021 21:01:45.330 * Module 'bf' loaded from /root/redis-6.2.1/RedisBloom/redisbloom.so
10066:M 22 Mar 2021 21:01:45.338 # Module /root/redis-6.2.1/redis-cell/libredis_cell.so failed to load: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by /root/redis-6.2.1/redis-cell/libredis_cell.so)
10066:M 22 Mar 2021 21:01:45.338 # Can't load module from /root/redis-6.2.1/redis-cell/libredis_cell.so: server aborting
"redis.log" 49L, 4925C
mkdir redis-cell

# 在该位置 下载包
wget https://github.com/brandur/redis-cell/releases/download/v0.2.5/redis-cell-v0.2.5-x86_64-unknown-linux-gnu.tar.gz

# 解压
tar -zxvf redis-cell/releases/download/v0.2.5/redis-cell-v0.2.5-x86_64-unknown-linux-gnu.tar.gz

# pwd 复制位置 修改redis.conf文件 加载新模块
vim redis.conf
# :/loadm 就能找到了
loadmodule pwd刚才的地址/libredis_cell.so
# esc :wq
# 重启redis
redis-server redis.conf
# 登陆进来 输入CL.THROTTLE命令 若是存在 说明安装成功
redis-cli -a 密码
CL.THROTTLE

3.命令

该模块只提供了一个命令:CL.THROTTLE

参数说明
CL.THROTTLE test 100 400 60 3

test: redis key

100: 官方叫max_burst,没理解什么意思,其值为令牌桶的容量 - 1, 首次执行时令牌桶会默认填满

400: 与下一个参数一起,表示在指定时间窗口内允许访问的次数

60: 指定的时间窗口,单位:秒

3: 表示本次要申请的令牌数,不写则默认为 1

以上命令表示从一个初始值为100的令牌桶中取3个令牌,该令牌桶的速率限制为400次/60秒。

4.返回值说明

127.0.0.1:6379> CL.THROTTLE test 100 400 60 3
1) (integer) 0
2) (integer) 101
3) (integer) 98
4) (integer) -1
5) (integer) 0
1: 是否成功,0:成功,1:拒绝

2: 令牌桶的容量,大小为初始值+1

3: 当前令牌桶中可用的令牌

4: 若请求被拒绝,这个值表示多久后才令牌桶中会重新添加令牌,单位:秒,可以作为重试时间

5: 表示多久后令牌桶中的令牌会存满

5.php实现redis-cell限流

<?php
class RedisLimit
{
    protected $redis =null;
    protected $key = "";
    protected $maxBurst =0;
    protected $speed = 0;
    protected $seconds = 0;
    protected $apply = 1;

    /**
     * RedisLimit constructor.
     * @param $key
     * @param $maxBurst 令牌桶容量
     * @param $speed 速度
     * @param $seconds
     * @param $apply 每次取出多少令牌
     */
    public function __construct($key,$maxBurst,$speed,$seconds,$apply)
    {
        $this->redis = new Redis();
        $this->redis->connect("127.0.0.1",6379);
        $this->key = $key;
        $this->maxBurst = $maxBurst;
        $this->speed = $speed;
        $this->seconds = $seconds;
        $this->apply = $apply;
    }
    public function passOrNot()
    {
        $ret = $this->redis->rawCommand("CL.THROTTLE",$this->key,$this->maxBurst,$this->speed,$this->seconds,$this->apply);
        return $ret[0];
    }
}
//60次5秒
$redis = new RedisLimit("limit",10,60,5,1);
for($i=0;$i<20;$i++) {
    print_r($redis->passOrNot());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值