限流脚本(window=60000ms,limit=100):local key = KEYS[1]
local now = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local limit = tonumber(ARGV[3])
redis.call('ZADD', key, now, now)
redis.call('ZREMRANGEBYSCORE', key, 0, now - window)
local count = tonumber(redis.call('ZCARD', key))
redis.call('EXPIRE', key, math.floor(window/1000))
if count <= limit then
return {1, limit - count}
else
return {0, 0}
end
执行示例:EVALSHA <sha> 1 rate:uid:123 1690000000000 60000 100

发表评论 取消回复