【Lua】Redis 自增并设置有效期
【Lua】Redis 自增并设置有效期
方案一 每次执行都会更新有效期
EVAL "local current = redis.call('INCRBY', KEYS[1], ARGV[1]);if tonumber(ARGV[2]) > 0 then redis.call('EXPIRE', KEYS[1], ARGV[2]) end;return current;" 1 mycounter 1 10
参数:
1 代表KEYS 有一个
mycounter KEYS[1]
1 自增数 ARGV[1]
10 有效期_秒 ARGV[2]
方案二 当前值 >= 某个值的时候 不更新有效期
EVAL "local current = redis.call('INCRBY', KEYS[1], ARGV[1]);if current >= tonumber(ARGV[3]) then return current end;if tonumber(ARGV[2]) > 0 then redis.call('EXPIRE', KEYS[1], ARGV[2]) end;return current;" 1 mycounter 1 10 2
参数:
前四个同 方案一
第五个参数 2 代表 当前值 >= 某个值的时候 不更新有效期