您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis-lInsert
发布时间:2021-12-28 22:45:01编辑:雪饮阅读()
lInsert
在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。
当列表不存在时,被视为空列表,不执行任何操作。
如果 key 不是列表类型则返回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->lInsert('key1', 'after', 'A', 'X', function ($r) {
var_dump($r); // 0
});
$redis->lPush('key1', 'A');
$redis->lPush('key1', 'B');
$redis->lPush('key1', 'C');
//在c之前插入x
$redis->lInsert('key1', 'before', 'C', 'X', function ($r) {
//返回插入后列表縂長度
var_dump($r); // 4
});
$redis->lRange('key1', 0, -1, function ($r) {
var_dump($r); // ['A', 'B', 'X', 'C']
});
};
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.
int(0)
int(4)
array(4) {
[0]=>
string(1) "X"
[1]=>
string(1) "C"
[2]=>
string(1) "B"
[3]=>
string(1) "A"
}
关键字词:workerman,redis,lInsert