user1663023
user1663023

Reputation:

Is there a way to increase ttl in redis?

I know there are several ways to set a specific ttl for a key, but is there a way to add some extra time for a key which has a counting down ttl?

Upvotes: 2

Views: 4689

Answers (2)

Hi computer
Hi computer

Reputation: 1121

  1. Good question
  2. there is no such command
  3. I think it is a bad idea to have a command like that, you have to be careful when you use it.
  4. Probably end up adding more time to the ttl than we expect. If you set it like 5 mins, the actual expire time will be close to 5 mins even if setting it multiple times in that request. But if you add multiple 5 mins to it, then we can`t be sure of the actual expire time

Upvotes: 1

for_stack
for_stack

Reputation: 22906

There's no built-in way to extend TTL. You need to get the current TTL, and then add some more TTL to it.

Wrap these two steps into a Lua script:

-- extend 300 seconds
eval 'local ttl = redis.call("TTL", "key") + 300; redis.call("EXPIRE", "key", ttl)' 0

Upvotes: 2

Related Questions