27

SET既存の ttl を削除せずにキーを再発行することは可能ですか? 現時点で私が知っている唯一の方法は、ttl を見つけて a を実行するSETEXことですが、それはあまり正確ではないようです。

4

6 に答える 6

19

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」

于 2014-02-20T04:01:53.947 に答える
19

KEEPTTLオプションは、redis>=6.0 でSETに追加されます。

https://redis.io/commands/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.
于 2020-01-17T16:23:57.673 に答える
13

INCRINCRBYDECRなどが役立つかもしれません。TTL は変更されません。

> setex test 3600 13
OK

> incr test
(integer) 14

> ttl test
(integer) 3554

http://redis.io/commands/INCR

于 2015-01-06T17:32:49.023 に答える