Can I set global TTL in redis? Instead of setting TTL every time I set a key.
I googled, but cannot found any clue. So it seems cannot be done?
Thanks.
Redis TTL command is used to get the remaining time of key expiry in seconds. Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.
There is no default TTL. By default, keys are set to live forever.
The max is 9223372036854775807 not 2147483647 for expire , try it.
When a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed. The key time to live can be updated or entirely removed using the EXPIRE and PERSIST command (or other strictly related commands).
No, Redis doesn't have a notion of a global/default TTL and yes, you do have to set it for each key independently. However, depending on your requirements and on what you're trying to do, there may be other ways to achieve your goal. Put differently, why do you need it?
For example, if you want to use Redis as a cache and not worry about having to remove "old" items, you can get simply by setting the maxmemory_policy
to allkey-lru
. This will evict the least recently used keys whenever Redis' memory is exhausted.
EDIT: for more information, see the helpful links in the comments below from @arganzheng and @Kristján, as well as the inline documentation in the redis.conf configuration file.
If you are setting a key , you can set the TTL in the same time : look at the set command
a side , you can done this by scripting (on linux like - for 60 seconds):
for k in `redis-cli --raw keys '*'` ; do redis expire $k 60;done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With