Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Multi-Set With a TTL

In redis there is a SETEX command that allows me to set a key that expires, is there a multi-set version of this command that also has a TTL?

both MSET and MSETNX commands do not have such an option.

like image 346
Ian Avatar asked May 07 '13 15:05

Ian


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

What is Max TTL in Redis?

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


2 Answers

I was also looking for this kind of operation. I didn't find anything, so I did it with MULTI/EXEC:

MULTI expire key1 expire key2 expire key3 EXEC 
like image 149
unreal Avatar answered Sep 20 '22 21:09

unreal


There is an issue for it back to 2012. For people who are wondering why they're not implement it.

Unfortunately, we're not going to add more commands that can work on multiple keys because they are inherently difficult to distribute. Instead, explicitly calling EXPIRE for every key you want to expire is much easier to distribute (you can route every command to a different server if needed). If you want to EXPIRE keys atomically, you can wrap multiple calls in a MULTI/EXEC block.


BTW, if the transaction is not required, try using pipeline instead of MULTI/EXEC for better performance.

Pipelining is not just a way to reduce the latency cost associated with the round trip time, it actually greatly improves the number of operations you can perform per second in a given Redis server.

like image 39
呂學洲 Avatar answered Sep 18 '22 21:09

呂學洲