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
效果如下:
