当前位置: 首页 > article >正文

Redis过期键监听

在 Redis 中,为了监听过期键事件,需要使用 Redis 的 Keyspace Notifications 功能。这一功能允许客户端订阅某些事件的发生,比如键过期、键删除等。

启用过期键监听

在 Redis 的配置文件 redis.conf 中,确保配置项 notify-keyspace-events 包含 'Ex''E' 表示启用过期事件通知,'x' 表示键事件。这可以通过直接在配置文件中设置:

notify-keyspace-events Ex

也可以在 Redis 运行时使用命令设置

CONFIG SET notify-keyspace-events Ex

订阅 Redis 过期事件

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {


    @Override
    public void onMessage(Message message, byte[] pattern) {

        });
    }
}

注意事项

  • 启用 Keyspace Notifications 会有一定的性能开销,因为 Redis 需要处理和传播事件通知。应在可能的情况下限制通知的范围,仅启用必要的事件类型。
  • 且通知是一次性的,没有ack机制,若收到通知后处理失败,将不再收到通知。需自行保证收到通知后处理成功。
  • 由于Redis key过期删除策略是定期+惰性删除,当key过多时,删除会有延迟。

如果对过期通知实时有要求的场景慎重考虑使用这种方式,引用Redis官方文档描述。

https://redis.io/docs/latest/develop/use/keyspace-notifications/#timing-of-expired-eventsicon-default.png?t=N7T8https://redis.io/docs/latest/develop/use/keyspace-notifications/#timing-of-expired-events

Timing of expired events

Keys with a time to live associated are expired by Redis in two ways:

  • When the key is accessed by a command and is found to be expired.
  • Via a background system that looks for expired keys in the background, incrementally, in order to be able to also collect keys that are never accessed.

The expired events are generated when a key is accessed and is found to be expired by one of the above systems, as a result there are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero.

If no command targets the key constantly, and there are many keys with a TTL associated, there can be a significant delay between the time the key time to live drops to zero, and the time the expired event is generated.

Expired (expired) events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero.

 

与生存时间(TTL)相关联的键通过两种方式在 Redis 中过期:

  1. 当一个命令访问该键并发现其已过期时。
  2. 通过一个后台系统,该系统在后台增量地查找过期的键,以便能够收集那些从未被访问过的键。

过期事件是在键被访问并通过上述某个系统发现过期时生成的。因此,Redis 服务器无法保证在键的生存时间降至零时就能立即生成过期事件。

如果没有命令持续访问该键,并且存在许多具有 TTL 的键,那么从键的生存时间降至零到生成过期事件之间可能会有显著的延迟。

过期(expired)事件是当 Redis 服务器删除该键时生成的,而不是在生存时间理论上达到零时生成的。


http://www.kler.cn/news/290315.html

相关文章:

  • 使用Java实现LRU缓存和LFU缓存
  • Oracle 19C 数据操纵语言DML
  • ES6中try-catch
  • 智能工厂监控升级:Sovit2D大屏展示和ARM计算机的完美搭档
  • 注意力机制
  • LangChain学习
  • 收徒弟了。
  • Oracle 常用函数大全
  • 深入理解区间调度问题:从贪心算法到动态规划的加权优化
  • SprinBoot+Vue实验室考勤管理微信小程序的设计与实现
  • UEFI开发——编写一个简单的PPI
  • FFmpeg源码:avcodec_descriptor_get函数分析
  • Flutter 仿iOS桌面悬浮球效果
  • 【数学建模备赛】Ep07:灰色预测模型
  • 随手笔记【五】
  • 【扇贝编程】使用Selenium模拟浏览器获取动态内容笔记
  • AI证件照生成神器颠覆传统,轻松驾驭考研、考公与签证申请
  • PHP + Redis 实现抽奖算法(ThinkPHP5)
  • Spring6梳理6——依赖注入之Setter注入
  • 【drools】Rulesengine构建及intelj配置
  • 通过组合Self-XSS + CSRF得到存储型XSS
  • 跨境电商代购系统中前台基本功能介绍:帮助更快的了解跨境代购业务
  • 注册登陆(最新版)
  • IOS 18 发现界面(UITableView)Banner轮播图实现
  • 【话题】提升开发效率的秘密武器:探索高效编程工具
  • SpinalHDL之BlackBox(下篇)
  • C#如何使用外部别名Extern alias
  • 单向链表与双向链表
  • 8逻辑回归的代价函数
  • HTTP与TCP的关系是什么?HTTP 的端口有什么意义?