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


    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
    ";
        echo "Channel: $chan
    ";
        echo "Payload: $msg
    
    ";
        //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

    效果如下:

    è¿éåå¾çæè¿°

  • 相关阅读:
    grep用法小结
    观察者模式
    类之间的关系
    Linux——makefile编写
    探索C++多态和实现机理
    进程间通信——管道
    C++继承引入的隐藏与重写
    Linux 文件读写操作与磁盘挂载
    xShell终端下中文乱码问题
    深入理解C++ new/delete, new []/delete[]动态内存管理
  • 原文地址:https://www.cnblogs.com/idjl/p/9610538.html
Copyright © 2020-2023  润新知