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

相關文章
相關標籤/搜索