下面介绍两种方法
zset
代码语言:javascript复制function uuid(){
$str = "123456790abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$uuid = "";
for ($i = 0; $i < 10; $i ) {
$uuid .= $str[mt_rand(0, strlen($str) - 1)];
}
return $uuid;
}
$time = time();
$key='limit:100';
$val = uuid();
$redis = new Redis();
$redis->connect("docker-redis", 6379);
$ret = $redis->multi(2)
->zRemRangeByScore($key, 0, $time - 60)
->zAdd($key, time(), $val)
->zCard($key)
->exec();
if ($ret[2] > 10) {
echo 'false, 每分钟最多访问10次';
return false;
}
echo 'ok';
redis-cell
Redis 4.0提供了一个限流Redis模块,称为Redis-Cell。该模块使用了漏斗算法,并提供了原子的限流指定。
https://github.com/brandur/redis-cell/releases/download/v0.3.0/redis-cell-v0.3.0-x86_64-unknown-linux-gnu.tar.gz
代码语言:javascript复制vi redis.conf
loadmodule /data/modules/libredis_cell.so
root@3afcc7091943:/data# redis-cli
127.0.0.1:6379> CL.THROTTLE user123 15 30 60 1
CL.THROTTLE user123 15 30 60 1
▲ ▲ ▲ ▲ ▲
| | | | └───── apply 1 token (default if omitted)
| | └──┴─────── 30 tokens / 60 seconds
| └───────────── 15 max_burst
└─────────────────── key "user123"
$redis = new Redis();
$redis->connect("docker-redis", 6379);
//10次1秒
for($i=0;$i<120;$i ) {
$ret = $redis->rawCommand("CL.THROTTLE", 'limit', 100, 10, 1, 1);
echo date('Y-m-d H:i:s').': '.($ret[0]===0?'pass':'no').PHP_EOL;
}