1. Redis Lua PHP脚本
function limit()
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$luaScript = <<<LUA
local times = redis.call('incr',KEYS[1])
if times == 1 then
redis.call('expire',KEYS[1], ARGV[1])
end
if times > tonumber(ARGV[2]) then
return 0
end
return 1
LUA;
$sha = $redis->script("load", $luaScript);
$result = $redis->evalSha($sha, Array('rate', 10, 3), 1);
}
var_dump(limit());
2. limit() 返回值0、1
Array('rate', 10, 3)
rate Key, 10, 3 10s内访问3次,3次内返回值1,大于3次0