【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? 代表?當前值 >=?某個值的時候?不更新有效期
?