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

1.我们先订阅频道名为 redisChat 
 

2.现在,我们重新开启个 redis 客户端,然后在同一个频道 redisChat 发布消息,订阅者就能接收到消息。 
 

接收到的消息如下: 
 

3.Key过期事件的Redis配置 

这里需要配置 notify-keyspace-events 的参数为 “Ex”。x 代表了过期事件。notify-keyspace-events “Ex” 保存配置后,重启Redis服务,使配置生效。

PHP redis实现订阅键空间通知

redis实例化类:redis.class.php

//遇到类别重复的报错,所有叫Redis2
class Redis2
{
private $redis; public function __construct($host = '127.0.0.1', $port = 6379)
{
$this->redis = new Redis();
$this->redis->connect($host, $port);
} public function setex($key, $time, $val)
{
return $this->redis->setex($key, $time, $val);
} public function set($key, $val)
{
return $this->redis->set($key, $val);
} public function get($key)
{
return $this->redis->get($key);
} public function expire($key = null, $time = 0)
{
return $this->redis->expire($key, $time);
} public function psubscribe($patterns = array(), $callback)
{
$this->redis->psubscribe($patterns, $callback);
} public function setOption()
{
$this->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
} }

过期事件的订阅:psubscribe.php

require_once './Redis.class.php';
$redis = new \Redis2();
// 解决Redis客户端订阅时候超时情况
$redis->setOption();
$redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback');
// 回调函数,这里写处理逻辑
function keyCallback($redis, $pattern, $chan, $msg)
{
echo "Pattern: $pattern\n";
echo "Channel: $chan\n";
echo "Payload: $msg\n\n";
//keyCallback为订阅事件后的回调函数,这里写业务处理逻辑,
//比如前面提到的商品不支付自动撤单,这里就可以根据订单id,来实现自动撤单
}

设置过期事件:index.php

require_once './Redis.class.php';
$redis = new \Redis2();
$order_id = 123;
$redis->setex('order_id',10,$order_id);

先用命令行模式执行 psubscribe.php

在浏览器访问 index.php

效果如下:

订单超时、活动过期解决方案:php监听redis key失效触发回调事件的更多相关文章

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

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

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

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

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

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

  4. Node.js 中监听 redis key 过期事件

    It is in fact possible to listen to the “expired” type keyevent notification using a subscribed clie ...

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

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

  6. 【转】Android开发20——单个监听器监听多个按钮点击事件

    原文网址:http://woshixy.blog.51cto.com/5637578/1093936 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...

  7. jQuery-使用hover(fn,fn)函数监听mouseover和mouseout两个事件

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. VueApp监听手机物理返回键的事件

    代码 第一步创建js文件夹子 在main里面引用   JS文本内容如下 //监听手机物理返回键的事件 document.addEventListener('plusready', function() ...

  9. SpringBoot监听redis过期key

    开启过期监听 vim /etc/redis.conf 取消notify-keyspace-events Elg的注释 pom.xml 添加: <dependency> <groupI ...

随机推荐

  1. 程序移植到AUTOCAD2013笔记

    1:需要引用acmgd.dll acdbmgd.dll,AcCoreMdg.dll, accui.dll 四个dll 2: 2010下的的acmgd.dll被拆分为acmgd.dll和AcCoreMd ...

  2. const与指针

    C++中const与指针 1.常指针: ; int * const pInt = &x; 其中PInt是常指针,pInt的值无法改变,但其指向的内容可以改变. 2.指向常量的指针 有两种写法: ...

  3. Spring Boot1.5X升级到2.0

    配置文件 大量的Servlet专属的server.* properties被移到了server.servlet下 拦截器 public class MyWebMvcConfigurerAdapter ...

  4. node.js、git、bootstrap等安装配置

    纯记录 一,安装node.js 1 官方网址 http://nodejs.org/  点击install 下载node-v0.10.22-x86.msi 2 安装,修改安装目录到d盘,一路next,无 ...

  5. Testbench学习——$fopen/$display/$fclose

    昨天在用Vivado写Testbench顶层时,为了以后便于数据的存储导出分析,需要用的文件数据记录的功能,于是,下面谈谈$fopen/$display/$fclose这三者的用法. $fopen—— ...

  6. Redis—数据结构之list

    Redis的列表对象底层所使用的数据结构其中之一就是list. list Redis的list是一个双端链表,其由3部分构成:链表节点.链表迭代器.链表.这一设计思想和STL的list是一样的,STL ...

  7. Ubuntu下软件安装方式、PATH配置、查找安装位置

    Ubuntu 18.04, 安装方式 目前孤知道的Ubuntu下安装软件方式有3种(命令): 1.make 2.apt/apt-get 3.dpkg 方式1基于软件源码安装,需要经历配置(可选).编译 ...

  8. tf.reduce_sum函数

    >>> x=[[1,2,3],[23,13,213]] >>> xx=tf.reduce_sum(x) >>> sess.run(xx) 255 ...

  9. Little C Loves 3 I

    CF#511 div2 A 现场掉分赛(翻车),就是这道题被叉了...qwq 其实就是一道水题: 因为CF有spj,所以直接构建特殊情况就行了. 当 n 是3的倍数的时候,显然 1,1,(n-2) 显 ...

  10. github后端开发面试题大集合(三)

    作者:小海胆链接:https://www.nowcoder.com/discuss/3616来源:牛客网 13.软件架构相关问题: 什么情况下缓存是没用的,甚至是危险的? 为什么事件驱动的架构能提高可 ...