Home  >  Article  >  Backend Development  >  PHP listens for redis key failure and triggers callback events

PHP listens for redis key failure and triggers callback events

藏色散人
藏色散人forward
2020-01-22 14:54:073611browse

PHP listens for redis key failure and triggers callback events

Solution for order timeout and activity expiration: PHP listens for redis key failure and triggers callback events

Redis 2.8. Available after version 0, keyspace messages (Redis Keyspace Notifications), combined with SUBSCRIBE after version 2.0.0, can complete the operation of this scheduled task. The timing unit is seconds.

1. Let’s first subscribe to the channel named redisChat

PHP listens for redis key failure and triggers callback events

2. Now, we reopen a redis client, and then Publish messages on the same channel redisChat, and subscribers can receive the messages.

PHP listens for redis key failure and triggers callback events

The message received is as follows:

PHP listens for redis key failure and triggers callback events

3. Redis configuration for Key expiration event

Here you need to configure the notify-keyspace-events parameter to "Ex". x represents an expiration event. notify-keyspace-events “Ex” After saving the configuration, restart the Redis service to make the configuration take effect.

PHP redis implements subscription key space notification

redis instantiation class:

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);
    }
 
}

Subscription for expired events:

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 "Payl
    oad: $msg\n\n";
    //keyCallback为订阅事件后的回调函数,这里写业务处理逻辑,
    //比如前面提到的商品不支付自动撤单,这里就可以根据订单id,来实现自动撤单 
}

Set expired events:

index.php

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

First execute psubscribe.php in command line mode

Access index.php

in the browser. The effect is as follows:

PHP listens for redis key failure and triggers callback events

For more related php knowledge, please visit php tutorial!

The above is the detailed content of PHP listens for redis key failure and triggers callback events. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete