PHP-Redis-Lua脚本解决访问限制

1. Redis Lua PHP脚本redis


function limit()lua

{spa

$redis = new Redis();ip

$redis->connect('127.0.0.1', 6379);it


$luaScript = <<<LUAio

local times = redis.call('incr',KEYS[1])function

if times == 1 thenim

   redis.call('expire',KEYS[1], ARGV[1])call

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

相关文章
相关标签/搜索