您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis-rPushX
发布时间:2021-12-29 21:34:37编辑:雪饮阅读()
rPushX
将一个值插入到已存在的列表尾部(最右边)并返回列表的长度。如果列表不存在,操作无效,返回0。 当列表存在但不是列表类型时返回false。
实例:
<?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('key1');
$redis->rPushX('key1', 'A', function ($r) {
var_dump($r); // 0
});
$redis->rPush('key1', 'B', function ($r) {
var_dump($r); // 1
});
$redis->rPushX('key1', 'C', function ($r) {
var_dump($r); // 2
});
$redis->lRange('key1',0,-1, function ($r) {
var_dump($r); //['B','C']
});
};
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.
int(0)
int(1)
int(2)
array(2) {
[0]=>
string(1) "B"
[1]=>
string(1) "C"
}
关键字词:workerman,redis,rPushX