Redis 中的绝对缓存和滑动缓存 [英] Absolute and Sliding Caching In Redis

查看:130
本文介绍了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 :设置密钥超时.
  • EXPIREAT:与之前相同,但采用绝对 Unix 时间戳(自 1970 年 1 月 1 日以来的秒数).
  • TTL :返回有超时的键的剩余生存时间
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆