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

https://www.cnblogs.com/liu-bear/p/11285147.html

场景:如1.用户下订单后,需要在15分钟内完成支付,否则订单失效关闭。
2.用户在完成订单后,如果没有人评论,过对应时间后,系统自动评论,并且完结。(为了防止尴尬、假象)

修改redis配置文件
找到机器上redis配置文件conf/redis.conf,新增一行  notify-keyspace-events Ex

最后的Ex代表 监听失效的键值

修改后效果如下图

代码效果:

redis.class.php类:(这里避免命名冲突,故命名Redis2

<?php
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

<?php
require_once './Redis.class.php';
$redis = new Redis2();
// 解决Redis客户端订阅时候超时情况
$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

<?php
require_once './Redis.class.php';
$redis = new Redis2();
$order_id = 123;
//设置一个时间为10秒的redis值
$redis->setex('order_id',10,$order_id);

https://www.cnblogs.com/idjl/p/9610538.html

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

效果如下:

è¿éåå¾çæè¿°

原文地址:https://www.cnblogs.com/yipianchuyun/p/14393572.html