Is it possible to set default ttl for all keys in redis?
Asked Answered
B

1

5

I've read redis config document but can't find such option.

I've searched and found that "by default, the key will simply live forever". And I want to change this default behavior eagerly.

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command. 
The EXPIRE family of commands is able to associate an expire to a given key, at the cost of some additional memory used by the key. When a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed.

http://redis.io/commands/expire (again)

Thanks in advance !

Beau answered 10/8, 2015 at 2:9 Comment(0)
M
11

Redis does not provide this ability - you'll have to explicitly set a TTL for every key. Note that updating a key resets its TTL, so you'll have to re-set it accordingly.

Morgue answered 10/8, 2015 at 5:17 Comment(3)
Whilst this is not possible in redis, any language wrapper could polyfill this by having methods on the Client implementation where the connection lives to persist a default ttlHuehuebner
@MrMesees, that's not true for all commands - the atomic operation of writing a list of keys doesn't have the expiry parameterMiscreated
Interesting; but why would you want the atomic operation to write a list of keys? Are you talking about MSET?Huehuebner

© 2022 - 2024 — McMap. All rights reserved.