mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 18:30:16 +08:00
18 lines
459 B
Lua
18 lines
459 B
Lua
-- app/Cache/Redis/Script/rate_limit.lua
|
|
local key = KEYS[1]
|
|
local limit = tonumber(ARGV[1])
|
|
local window = tonumber(ARGV[2])
|
|
local current = redis.call('GET', key)
|
|
local remaining = 0
|
|
if current then
|
|
remaining = tonumber(current) - 1
|
|
if remaining >= 0 then
|
|
redis.call('DECR', key)
|
|
else
|
|
remaining = -1
|
|
end
|
|
else
|
|
remaining = limit - 1
|
|
redis.call('SET', key, remaining, 'EX', window)
|
|
end
|
|
return {remaining >= 0, remaining} |