Message queue based on Redis and PHP

[1] Install Redis (omitted)

 

[2] Install phpredis (omitted)

 

[3] Redis' message subscription and publishing are required. If you want to monitor the key value expiration event, you must modify the configuration file and find the comment of notify-keyspace-events Ex to remove. (Applicable scenarios, the order will be automatically canceled 30 minutes after the following order).

 

[4] Release the message, publish the message to the test1 channel

$redis = new Redis();
$isLink = $redis->connect('127.0.0.1', 6379);
if ($isLink) {
    $redis->publish('test1', 'msg1 \n');
}

 

[5] Message subscription, to receive messages published by publish and events of Key expiration, put them on the command line to run

ini_set('default_socket_timeout', -1);
try{
    $redis = new Redis();
    $ res = $ redis-> pconnect ('127.0.0.1', 6379, 0);
    $redis->psubscribe(array('test1', '__keyevent@0__:expired'), 'callback');
} catch (Exception $exc) {
    echo $exc->getMessage();
}

// Callback function, write processing logic here
function callback($instance, $pattern, $channelName, $message)
{
    echo $channelName, "==>", $message, PHP_EOL;
}

 At this point, complete

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326024730&siteId=291194637
Recommended