Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set global TTL in redis?

Tags:

redis

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.

like image 305
Sato Avatar asked Sep 02 '14 07:09

Sato


People also ask

Can we set TTL in Redis?

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.

What is the default TTL for Redis?

There is no default TTL. By default, keys are set to live forever.

What is Max TTL in Redis?

The max is 9223372036854775807 not 2147483647 for expire , try it.

What happens when TTL expires Redis?

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


2 Answers

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.

like image 196
Itamar Haber Avatar answered Oct 02 '22 20:10

Itamar Haber


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 
like image 20
Philippe T. Avatar answered Oct 02 '22 18:10

Philippe T.