您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件 -sort
发布时间:2021-12-26 21:55:40编辑:雪饮阅读()
前番了解了sort在redis中的原生使用,那麽在workerman的redis組件中又是如何使用的呢?
sort
sort命令可以对list、set和sorted set的元素进行排序。
原型:sort($key, $options, $callback);
其中options是以下可选的key和值
$options = [
'by' => 'some_pattern_*',
'limit' => [0, 1],
'get' => 'some_other_pattern_*', // or an array of patterns
'sort' => 'asc', // or 'desc'
'alpha' => true,
'store' => 'external-key'
];
那麽具體的實例:
<?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('s');
$redis->sAdd('s', 5);
$redis->sAdd('s', 4);
$redis->sAdd('s', 2);
$redis->sAdd('s', 1);
$redis->sAdd('s', 3);
$redis->sAdd('s', 0);
$redis->sort('s', [], function ($result) {
//升序排
var_dump($result);
});
$redis->sort('s', ['sort' => 'desc'], function ($result) {
//降序排
var_dump($result);
});
$redis->sort('s', ['sort' => 'desc', 'store' => 'out'], function ($result) {
//降序排結果保存,返回元素個數
var_dump($result);
});
};
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.
array(6) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(1) "3"
[4]=>
string(1) "4"
[5]=>
string(1) "5"
}
array(6) {
[0]=>
string(1) "5"
[1]=>
string(1) "4"
[2]=>
string(1) "3"
[3]=>
string(1) "2"
[4]=>
string(1) "1"
[5]=>
string(1) "0"
}
int(6)
array(6) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(1) "3"
[4]=>
string(1) "4"
[5]=>
string(1) "5"
}
array(6) {
[0]=>
string(1) "5"
[1]=>
string(1) "4"
[2]=>
string(1) "3"
[3]=>
string(1) "2"
[4]=>
string(1) "1"
[5]=>
string(1) "0"
}
int(6)
array(6) {
[0]=>
string(1) "0"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(1) "3"
[4]=>
string(1) "4"
[5]=>
string(1) "5"
}
array(6) {
[0]=>
string(1) "5"
[1]=>
string(1) "4"
[2]=>
string(1) "3"
[3]=>
string(1) "2"
[4]=>
string(1) "1"
[5]=>
string(1) "0"
}
int(6)
那麽最終store存儲的排序結果集如:
127.0.0.1:6379> lrange out 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
6) "0"
关键字词:workerman,redis,sort