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.
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.
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).
The max is 9223372036854775807 not 2147483647 for expire , try it.
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
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.
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