您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis-sPop
发布时间:2021-12-29 21:42:21编辑:雪饮阅读()
sPop
移除集合中的指定 key 的一个或多个随机元素,移除后会返回移除的元素。
当集合不存在或是空集时,返回 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');
$redis->del('key1');
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3');
//随机移除一个
$redis->sPop('key1', function ($r) {
//返回被移除元素
var_dump($r);
});
$redis->sAdd('key2', ['member1', 'member2', 'member3']);
//随机移除多个
$redis->sPop('key2', 3, function ($r) {
//返回被移除元素
var_dump($r);
});
};
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(7) "member1"
array(3) {
[0]=>
string(7) "member3"
[1]=>
string(8) "member13"
[2]=>
string(7) "member1"
}
^CWorkerman[index.php] stopping ...
Workerman[index.php] has been stopped
[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(7) "member3"
array(3) {
[0]=>
string(7) "member3"
[1]=>
string(7) "member2"
[2]=>
string(7) "member1"
}
关键字词:workerman,redis,sPop