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

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 {@Overridepublic 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 服务器删除该键时生成的,而不是在生存时间理论上达到零时生成的。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【mysql】mysql之日志
  • kafka3.7.1 单节点 KRaft部署测试发送和接收消息
  • oceanbase OBCE 第四章实验 事务与远程执行
  • 欧科云链OKLink受邀参与WebX ,旗下EaaS助力项目方“弯道超车”
  • 首款AI手机就要来了?你知道iPhone 16都有哪些AI功能吗?
  • 05-最新PyCharm安装详细教程及pycharm配置
  • 【Spring Boot 3】【Web】配置HTTPS
  • Power Automate向Power Apps传Table数据
  • uniapp小程序怎么判断滑动的方向
  • 手机FM LNA方案设计
  • java导出Excel接口
  • 恒润达生无商业化持续亏损,现金流连年为负募资超25亿
  • git进阶·团队开发的时候为何要创建临时分支来修复bug
  • 压缩大型语言模型 LLMs
  • 简单的棒棒图绘制教程
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • eclipse(luna)创建web工程
  • exports和module.exports
  • Idea+maven+scala构建包并在spark on yarn 运行
  • Median of Two Sorted Arrays
  • Python利用正则抓取网页内容保存到本地
  • TypeScript实现数据结构(一)栈,队列,链表
  • 类orAPI - 收藏集 - 掘金
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • #100天计划# 2013年9月29日
  • (2020)Java后端开发----(面试题和笔试题)
  • (2024最新)CentOS 7上在线安装MySQL 5.7|喂饭级教程
  • (3)选择元素——(17)练习(Exercises)
  • (接口自动化)Python3操作MySQL数据库
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (转)c++ std::pair 与 std::make
  • .form文件_SSM框架文件上传篇
  • .NET : 在VS2008中计算代码度量值
  • .net framework profiles /.net framework 配置
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .net6使用Sejil可视化日志
  • .NET未来路在何方?
  • .NET周刊【7月第4期 2024-07-28】
  • .sh
  • 。。。。。
  • @Query中countQuery的介绍
  • @RequestParam详解
  • [.NET]桃源网络硬盘 v7.4
  • [2016.7.Test1] T1 三进制异或
  • [AIGC] MySQL存储引擎详解
  • [AutoSar]BSW_Com02 PDU详解
  • [BZOJ2850]巧克力王国
  • [corCTF 2022] CoRJail: From Null Byte Overflow To Docker Escape
  • [CTO札记]盛大文学公司名称对联
  • [EFI]Atermiter X99 Turbo D4 E5-2630v3电脑 Hackintosh 黑苹果efi引导文件