您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis-rPopLPush
发布时间:2021-12-29 21:33:30编辑:雪饮阅读()
rPopLPush
用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。
实例:
<?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');
$redis->del('x', 'y');
$redis->lPush('x', 'abc');
$redis->lPush('x', 'def');
$redis->lPush('y', '123');
$redis->lPush('y', '456');
$redis->rPopLPush('x', 'y', function ($r) {
var_dump($r); // abc
});
$redis->lRange('x', 0, -1, function ($r) {
var_dump($r); // ['def']
});
$redis->lRange('y', 0, -1, function ($r) {
var_dump($r); // ['abc', '456', '123']
});
};
Worker::runAll();
实例运行并有http请求时:
[root@localhost workerman]# php index.php start
Workerman[index.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
------------------------------------------ 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.
string(3) "abc"
array(1) {
[0]=>
string(3) "def"
}
array(3) {
[0]=>
string(3) "abc"
[1]=>
string(3) "456"
[2]=>
string(3) "123"
}
关键字词:workerman,redis,rPopLPush