redishiredis

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


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.

See EXPIRE docs for more details.


Solution

  • 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.