您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis-bRPopLPush
发布时间:2021-12-28 22:39:10编辑:雪饮阅读()
bRPopLPush
从列表中取出最后一个元素,并插入到另外一个列表的头部; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。如果超时则返回null。
實例:
<?php
use Workerman\Worker;
use Workerman\Redis\Client;
use Workerman\Connection\TcpConnection;
use Workerman\Timer;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:6161');
global $offset;
$worker->onWorkerStart = function() {
global $offset;
$offset=0;
};
$worker->onMessage = function(TcpConnection $connection, $data) {
$redis = new Client('redis://127.0.0.1:6379');
$redis2 = new Client('redis://127.0.0.1:6379');
$redis->del(['key1', 'key2']);
//第三個參數為超時時間
$redis->bRPopLPush('key1', 'key2', 2, function ($r) {
//返回取出的值
var_export($r);
});
Timer::add(2, function () use ($redis2) {
$redis2->lpush('key1', 'a');
$redis2->lRange('key2', 0, -1, function ($r) {
var_dump($r);
});
}, null, false);
};
Worker::runAll();
實例運行并有http客戶端請求時:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php channelServer.php start
Workerman[channelServer.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:6161 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
'a'array(1) {
[0]=>
string(1) "a"
}
关键字词:workerman,redis,bRPopLPush