質問

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.

役に立ちましたか?

解決 2

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.

That's sad we cant set expire with mset, ahead a solution for who is working with nodejs and redis lib:

// expires the key at next mid-night
let now = moment()
let endOfDay = moment().endOf('day')
let timeToLiveInSeconds = endOfDay.diff(now, 'seconds')

redisClient.expire(keyName, timeToLiveInSeconds)

I hope it helps

EVAL "<multi_ttl_script>" N key1 key2 ... value1 ttl1 value2 ttl2 ...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top