Node.js 中监听 redis key 过期事件
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 过期事件的更多相关文章
- Spring boot实现监听Redis key失效事件实现和其它方式
需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 用户绑定隐私号码当订单结束取消绑定等 解决方案1: 可以利用redis自带的key自动过期机制,下单时将订单id写入redis,过 ...
- 【Redis系列】Spring boot实现监听Redis key失效事件
talk is cheap, show me the code. 一.开启Redis key过期提醒 方式二:修改配置文件 redis.conf # 默认 notify-keyspace-events ...
- SpringBoot实现监听redis key失效事件
需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态 ...
- springboot使用Redis,监听Redis键过期的事件设置与使用代码
我使用的是Windows下的Redis服务,所以一下Redis设置都是在Windows平台进行. 1.修改Redis配置文件 1.1:Windows下的Redis存在两个配置文件 修改带有servic ...
- 订单超时、活动过期解决方案:php监听redis key失效触发回调事件
Redis 的 2.8.0 版本之后可用,键空间消息(Redis Keyspace Notifications),配合 2.0.0 版本之后的 SUBSCRIBE 就能完成这个定时任务的操作了,定时的 ...
- [转]Node.js中koa使用redis数据库
本文转自:https://blog.csdn.net/offbye/article/details/52452322 Redis是一个常用的Nosql数据库,一般用来代替Memcached做缓存服务, ...
- node.js绑定监听事件EventEmitter类
Node.js 有多个内置的事件,我们可以通过引入 events 模块,并通过实例化 EventEmitter 类来绑定和监听事件,如下: // 引入 events 模块 var events = r ...
- 如何利用redis key过期事件实现过期提醒
https://blog.csdn.net/zhu_tianwei/article/details/80169900 redis自2.8.0之后版本提供Keyspace Notifications功能 ...
- Redis Key过期事件
解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态,如果未支付,则进行处理但是key过期了redis有通知吗?答案是肯定 ...
随机推荐
- thinkphp不读取.env文件的键对值
第一:$_ENV会为空,其原因通常是php的配置文件php.ini的配置项为: :variables_order :Default Value: “EGPCS” :Development Value: ...
- flask之视图函数从前端接收数据的方法
一:从前端接收查询字符串 query-string 注意:get和post都可以在url后面添加查询字符串?a=1&b=2 测试工具:postman 1:get方式接收 视图函数 from ...
- redis专题
1.Linux安装redis 2.redis持久化 3.redis配置 4.SpringBoot整合Redis发布订阅 5.redis事务 5.1.redis事务介绍 5.2. redisTempla ...
- maven推送本地包到私服
前置要求:配置正确的settings.xml maven 推送 本地jar 到私服的命令示例: mvn deploy:deploy-file -DgroupId=com.oracle -Dartifa ...
- @ApiModelProperty
@ApiModelProperty用法 @ApiModelProperty()用于方法,字段: 表示对model属性的说明或者数据操作更改 value–字段说明 name–重写属性名字 dataT ...
- python 全栈开发,Day53(jQuery的介绍,jQuery的选择器,jQuery动画效果)
01-jQuery的介绍 1.为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题. ...
- [CSP-S模拟测试]:嘟嘟噜(约瑟夫问题)
题目描述 由于众所周知的原因,冈部一直欠真由理一串香蕉.为了封上真由理的嘴,冈部承诺只要真由理回答出这个问题,就给她买一车的香蕉:一开始有$n$个人围成一个圈,从$1$开始顺时针报数,报出$m$的人被 ...
- leetcode 217. 存在重复元素 (python)
给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1]输出: true示 ...
- Python笔记(十七)_面向对象编程
面向对象编程 概念:简称OOP,是一种程序设计思想:OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数 面向对象的设计思想:抽象出类class,根据类class创建实例对象instan ...
- Item-Based Collaborative Recommender System
与User-Based Collaborative Recommender System认为‘类似的用户会对同一个item给出类似的打分’不同,Item-Based Collaborative Rec ...