It is in fact possible to listen to the “expired” type keyevent notification using a subscribed client to the specific channel and listening to its message event.

通过 subscribe client 可以监听 __keyevent@db__:expired 频道来接收过期的事件。

const redis = require('redis');
const CONF = {db:3};
var pub, sub;
//.: Activate "notify-keyspace-events" for expired type events
pub = redis.createClient(CONF);
pub.send_command('config', ['set','notify-keyspace-events','Ex'], SubscribeExpired);
//.: Subscribe to the "notify-keyspace-events" channel used for expired type events
function SubscribeExpired(e,r){
sub = redis.createClient(CONF);
const expired_subKey = '__keyevent@'+CONF.db+'__:expired'
sub.subscribe(expired_subKey,function(){
console.log(' [i] Subscribed to "'+expired_subKey+'" event channel : '+r);
sub.on('message',function (chan,msg){
console.log('[expired]',msg);
   })
TestKey();
  })
}
//.: For example (create a key & set to expire in 10 seconds)
function TestKey(){
pub.set('testing','redis notify-keyspace-events : expired')
pub.expire('testing',10)
}

Node.js 中监听 redis key 过期事件的更多相关文章

  1. Spring boot实现监听Redis key失效事件实现和其它方式

    需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 用户绑定隐私号码当订单结束取消绑定等 解决方案1: 可以利用redis自带的key自动过期机制,下单时将订单id写入redis,过 ...

  2. 【Redis系列】Spring boot实现监听Redis key失效事件

    talk is cheap, show me the code. 一.开启Redis key过期提醒 方式二:修改配置文件 redis.conf # 默认 notify-keyspace-events ...

  3. SpringBoot实现监听redis key失效事件

    需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态 ...

  4. springboot使用Redis,监听Redis键过期的事件设置与使用代码

    我使用的是Windows下的Redis服务,所以一下Redis设置都是在Windows平台进行. 1.修改Redis配置文件 1.1:Windows下的Redis存在两个配置文件 修改带有servic ...

  5. 订单超时、活动过期解决方案:php监听redis key失效触发回调事件

    Redis 的 2.8.0 版本之后可用,键空间消息(Redis Keyspace Notifications),配合 2.0.0 版本之后的 SUBSCRIBE 就能完成这个定时任务的操作了,定时的 ...

  6. [转]Node.js中koa使用redis数据库

    本文转自:https://blog.csdn.net/offbye/article/details/52452322 Redis是一个常用的Nosql数据库,一般用来代替Memcached做缓存服务, ...

  7. node.js绑定监听事件EventEmitter类

    Node.js 有多个内置的事件,我们可以通过引入 events 模块,并通过实例化 EventEmitter 类来绑定和监听事件,如下: // 引入 events 模块 var events = r ...

  8. 如何利用redis key过期事件实现过期提醒

    https://blog.csdn.net/zhu_tianwei/article/details/80169900 redis自2.8.0之后版本提供Keyspace Notifications功能 ...

  9. Redis Key过期事件

    解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态,如果未支付,则进行处理但是key过期了redis有通知吗?答案是肯定 ...

随机推荐

  1. 五、Ubuntu 16.04 安装PyCharm

    方法一,在终端用指令通过第三方源安装pycharm. 本文通过第三方源安装PyCharm,好处是升级方便. 添加源:    $ sudo add-apt-repository ppa:mystic-m ...

  2. java中的Excel导出功能

    public void exportExcel(Long activityId, HttpServletResponse response) throws IOException { // 获取统计报 ...

  3. Flutter-ListTile

    ListTile 通常用于在 Flutter 中填充 ListView.在这篇文章中,我将用可视化的例子来说明所有的参数. title title 参数可以接受任何小部件,但通常是文本小部件 List ...

  4. java 关键字volatile

    一.Java内存模型 想要理解volatile为什么能确保可见性,就要先理解Java中的内存模型是什么样的. Java内存模型规定了所有的变量都存储在主内存中.每条线程中还有自己的工作内存,线程的工作 ...

  5. js arguments参数

    在调用函数时,浏览器每次都会传递进两个隐含的参数:      1.函数的上下文对象 this      2.封装实参的对象 arguments         - arguments是一个类数组对象, ...

  6. 无法启用web调试服务器

    场景:vs2005   webservice 项目属性中:web : 使用IIS web  服务器  http://localhost/WSMA   --- >自动创建虚拟目录 点击调试的时候F ...

  7. php str_word_count()函数 语法

    php str_word_count()函数 语法 作用:计算字符串中的单词数.大理石平规格 语法:str_word_count(string,return,char) 参数: 参数 描述 strin ...

  8. 代码片段快捷键 CodeSnippets

    CodeSnippets https://github.com/jaydee3/CodeSnippets These are my Xcode 4 CodeSnippets. To use them, ...

  9. HDU 5634 Rikka with Phi

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 ------------------------------------------------ ...

  10. pve之daemon

    pmxcfs The Proxmox Cluster file system (“pmxcfs”) is a database-driven file system for storing confi ...