SET
既存の ttl を削除せずにキーを再発行することは可能ですか? 現時点で私が知っている唯一の方法は、ttl を見つけて a を実行するSETEX
ことですが、それはあまり正確ではないようです。
6 に答える
Redis のドキュメントによると、キーが上書きされるため、このSET
コマンドは TTL を削除します。
ただし、コマンドを使用しEVAL
て Lua スクリプトを評価し、自動的に実行することができます。
以下のスクリプトは、キーの TTL 値をチェックします。値が正の場合はSETEX
、新しい値と残りの TTL を使用して呼び出します。
local ttl = redis.call('ttl', ARGV[1]) if ttl > 0 then return redis.call('SETEX', ARGV[1], ttl, ARGV[2]) end
例:
> キー 123 を設定
わかった
> 期限切れキー 120
(整数) 1
...数秒後
> ttl キー
(整数) 97
> eval "local ttl = redis.call('ttl', ARGV[1]) if ttl > 0 then return redis.call('SETEX', ARGV[1], ttl, ARGV[2]) end" 0 key 987
わかった
> ttl キー
96
> 鍵を入手
「987」
KEEPTTLオプションは、redis>=6.0 でSETに追加されます。
https://github.com/antirez/redis/pull/6679
The SET command supports a set of options that modify its behavior:
EX seconds -- Set the specified expire time, in seconds.
PX milliseconds -- Set the specified expire time, in milliseconds.
NX -- Only set the key if it does not already exist.
XX -- Only set the key if it already exist.
(!) KEEPTTL -- Retain the time to live associated with the key.
INCR
、INCRBY
、DECR
などが役立つかもしれません。TTL は変更されません。
> setex test 3600 13
OK
> incr test
(integer) 14
> ttl test
(integer) 3554