Redis 中的绝对缓存和滑动缓存 [英] Absolute and Sliding Caching In Redis
问题描述
我想在 Redis 中实现绝对缓存和滑动缓存.有谁有任何资源链接,那么它会有所帮助
I want to implement Absolute and Sliding Caching In Redis. Does anyone have any resource link then it will be helpful
推荐答案
Redis 已经有很多用于此的命令:
Redis already have many commands for this :
- EXPIRE : Set a timeout on key.
- EXPIREAT : Same as previous but takes an absolute Unix timestamp (seconds since January 1, 1970).
- TTL : Returns the remaining time to live of a key that has a timeout
关于 Redis 上的过期,您必须了解的一件重要事情:只有在使用 SET 或 GETSET 删除或覆盖键时才会清除超时值.所有其他命令(INCR、LPUSH、HMSET 等)永远不会改变初始超时.
One important thing you have to know about Expiration on Redis : the timeout value is cleared only when the key is removed or overwritten using SET or GETSET. All others commands (INCR, LPUSH, HMSET, ...) will never change the initial timeout.
绝对过期是 Redis 使用 EXPIRE 的本机功能.要实现滑动过期,您只需在每个命令后重置为超时值.
Absolute expiration is a native feature of Redis using EXPIRE. To implement a sliding expiration you simply need to reset to timeout value after each command.
这样做的基本方法可能是
A basic way to do this could be
MULTI
GET MYKEY
EXPIRE MYKEY 60
EXEC
这篇关于Redis 中的绝对缓存和滑动缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!