php调用redis的scan,hscan,zscan 命令
使用redis擴(kuò)展的scan函數(shù),發(fā)現(xiàn)這個(gè)函數(shù)寫的很有問題,返回值并沒有游標(biāo),而且游標(biāo)初始值要設(shè)置為null而不是0,感覺用起來還是挺別捏的,直接放棄了,直接使用萬(wàn)能的rawCommand函數(shù)來執(zhí)行原生的redis命令。
可參考:https://blog.csdn.net/raoxiaoya/article/details/100515541
SCAN 命令用于迭代當(dāng)前數(shù)據(jù)庫(kù)中的數(shù)據(jù)庫(kù)鍵。
HSCAN 命令用于迭代哈希鍵中的鍵值對(duì)。
1、scan 命令
比如,總共有9個(gè)key,兩次調(diào)用 scan 命令返回值類似于
封裝函數(shù),整理數(shù)據(jù),最終返回key的數(shù)組。
public static function scanGetKeys($pattern, $count = 50){$ret = [];$iterator = 0;while (true) {$result = Redis::rawCommand("scan", $iterator, 'match', $pattern, 'count', $count);dump($result);if ($result === false) {break;}$ret = array_merge($ret, $result[1]);$iterator = $result[0];if($result[0] == 0){break;}}return $ret; }調(diào)用 $r1 = Read3Service::scanGetKeys('getReadtimeUserRank:*');
2、hscan 命令
如果你的hash表的key較多,那么也需要使用hscan命令,hscan 命令返回值類似于
對(duì)應(yīng)的是
封裝函數(shù),整理數(shù)據(jù),最終返回 key => value 的數(shù)組。
hscanGetKeyValue函數(shù)返回的是整理后的key => value 對(duì)。
調(diào)用 $list = RedisService::hscanGetKeyValue($key, '*');
3、zscan 命令
public static function zscanGetKeyValue($key, $pattern, $count = 50) {$ret = [];$iterator = 0;while (true) {$result = Redis::rawCommand("zscan", $key, $iterator, 'match', $pattern, 'count', $count);if ($result === false) {break;}$temp = $result[1];foreach($temp as $k => $v){if($k % 2 == 0){$ret[$v] = ''; // $k偶數(shù)為key}else{$ret[$temp[$k-1]] = $v; // $k奇數(shù)為value}}$iterator = $result[0];if($result[0] == 0){break;}}return $ret; }總結(jié)
以上是生活随笔為你收集整理的php调用redis的scan,hscan,zscan 命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: markdown多级列表
- 下一篇: python学习3:IDEL(pytho