|
@@ -1,4 +1,4 @@
|
|
-local key = KEYS[1]
|
|
|
|
|
|
+--local key = KEYS[1]
|
|
local now = tonumber(ARGV[1]) -- 当前时时间戳
|
|
local now = tonumber(ARGV[1]) -- 当前时时间戳
|
|
local ttl = tonumber(ARGV[2]) -- key的过期时间
|
|
local ttl = tonumber(ARGV[2]) -- key的过期时间
|
|
local expired = tonumber(ARGV[3]) -- 元素过期分数上限(用于移除过期时间窗口元素)
|
|
local expired = tonumber(ARGV[3]) -- 元素过期分数上限(用于移除过期时间窗口元素)
|
|
@@ -7,10 +7,10 @@ local limitCount = tonumber(ARGV[4]) --限制访问次数
|
|
-- 清除过期的数据
|
|
-- 清除过期的数据
|
|
-- 移除指定分数区间内的所有元素,expired 即已经过期的 score
|
|
-- 移除指定分数区间内的所有元素,expired 即已经过期的 score
|
|
-- 根据当前时间毫秒数 - 超时毫秒数,得到过期时间 expired
|
|
-- 根据当前时间毫秒数 - 超时毫秒数,得到过期时间 expired
|
|
-redis.call('zremrangebyscore', key, 0, expired)
|
|
|
|
|
|
+redis.call('zremrangebyscore', KEYS[1], 0, expired)
|
|
|
|
|
|
-- 获取 zset 中的当前元素个数
|
|
-- 获取 zset 中的当前元素个数
|
|
-local current = tonumber(redis.call('zcard', key))
|
|
|
|
|
|
+local current = tonumber(redis.call('zcard', KEYS[1]))
|
|
local next = current + 1
|
|
local next = current + 1
|
|
|
|
|
|
if next > limitCount then
|
|
if next > limitCount then
|
|
@@ -18,8 +18,8 @@ if next > limitCount then
|
|
return 0;
|
|
return 0;
|
|
else
|
|
else
|
|
-- 往 zset 中添加一个[value,score]均为当前时间戳的元素,[value,score]
|
|
-- 往 zset 中添加一个[value,score]均为当前时间戳的元素,[value,score]
|
|
- redis.call("zadd", key, now, now)
|
|
|
|
|
|
+ redis.call("zadd", KEYS[1], now, now)
|
|
-- 每次访问均重新设置 zset 的过期时间,单位毫秒
|
|
-- 每次访问均重新设置 zset 的过期时间,单位毫秒
|
|
- redis.call("pexpire", key, ttl)
|
|
|
|
|
|
+ redis.call("pexpire", KEYS[1], ttl)
|
|
return next
|
|
return next
|
|
end
|
|
end
|