简体   繁体   中英

Redis - decrease TTL by single command

Let's say I have redis record with TTL = 1 hour.

Then some event occurs and I want to reset the TTL of this item to min(current-TTL, 5min). So it would decrease the TTL to 5 mins. if it is not already lower.

The underlying use-case is, there can be too frequent invalidation of the cache and the "old" cache is almost as good as the fresh, if it is not older then 5mins. from the first change.

I know I can fetch the TTL in one command and update it with second, but would prefer to set it by single command for various reasons. Is there a way?

Edit: There will be many keys I would need to decrease by single command. So I would like to avoid data round-trips between redis and client library for each record.

There is no single command to do that, but you can wrap the logic in a server-side Lua script and invoke that with a single command. Refer to the EVAL command for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM