phpredis扫描不会返回下一个光标

nx7onnlm  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(386)

在我的laravel应用程序中,我使用 predis/predis 包可以使用redis进行缓存。最近,根据文档中的建议,我已经切换到phpredis,但是我使用scan的部分代码被破坏了。在predis中,当我使用这样的扫描时:

Redis::scan(0,'match',$pattern //Test:*);

我曾经得到这样的结果:

[
     "253952",
     [
       "Test::296589",
       "Test::299112",
       "Test::332487",
       "Test::320358",
     ],
   ]

在本例中,253952是下一个游标,我可以通过它再次扫描以获得下一批这样的数据 Redis::scan(253952,'match',$pattern //Test:*); 我可以循环运行一段时间,直到我到达终点:

$all_keys = [];
$keys = RedisManager::scan(0, 'match', $pattern);
while ($keys[0] !== "0") {
   foreach($keys[1] as $key) {
      $all_keys[] = $key;
   }
 $keys = RedisManager::scan($keys[0], 'match', $pattern);
}

自从我切换到phpredis,我就不能再遍历这个列表了。首先,语法似乎不同:

$it=null;
$keys = RedisManager::scan($it,$pattern);

我不确定将null作为迭代器传递给第一个参数有什么意义。它也只是返回:

[
 "Test::296589",
 "Test::299112"
]

这只是结果的一部分。它没有给我所有的东西,也没有给我下一个光标。有人能给我指路吗?

ttisahbt

ttisahbt1#

我换成了 Phpredis 在我的本地服务器上,并使其与以下代码片段一起工作。文档中说明了扫描的不同用法。

$redis = RedisManager::connection(); // initialize

$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); // please check documentation for other option
$keyList = [];
$iterator = null;

// you may update the count or match options according to your needs
while ($keys = $redis->scan($iterator, ['match' => 'Test:*', 'count' => 20])) {
    $iterator = $keys[0];
    foreach ($keys[1] as $key) {
        $keyList[] = $key;
    }
}

return $keyList;

在调试或开发时使用 monitor 命令。当这个片段工作时,它打印出这样的东西;

1603241935.261667 [0 127.0.0.1:52536] "SCAN" "0" "COUNT" "20" "MATCH" "Test:*"
1603241935.262241 [0 127.0.0.1:52536] "SCAN" "48" "COUNT" "20" "MATCH" "Test:*"
1603241935.263421 [0 127.0.0.1:52536] "SCAN" "424" "COUNT" "20" "MATCH" "Test:*"
1603241935.264476 [0 127.0.0.1:52536] "SCAN" "260" "COUNT" "20" "MATCH" "Test:*"
1603241935.265508 [0 127.0.0.1:52536] "SCAN" "100" "COUNT" "20" "MATCH" "Test:*"
1603241935.266405 [0 127.0.0.1:52536] "SCAN" "460" "COUNT" "20" "MATCH" "Test:*"
1603241935.267137 [0 127.0.0.1:52536] "SCAN" "258" "COUNT" "20" "MATCH" "Test:*"
1603241935.268271 [0 127.0.0.1:52536] "SCAN" "402" "COUNT" "20" "MATCH" "Test:*"
1603241935.269344 [0 127.0.0.1:52536] "SCAN" "42" "COUNT" "20" "MATCH" "Test:*"
1603241935.270328 [0 127.0.0.1:52536] "SCAN" "198" "COUNT" "20" "MATCH" "Test:*"
1603241935.275193 [0 127.0.0.1:52536] "SCAN" "342" "COUNT" "20" "MATCH" "Test:*"
1603241935.276690 [0 127.0.0.1:52536] "SCAN" "286" "COUNT" "20" "MATCH" "Test:*"
1603241935.279386 [0 127.0.0.1:52536] "SCAN" "510" "COUNT" "20" "MATCH" "Test:*"
1603241935.281084 [0 127.0.0.1:52536] "SCAN" "81" "COUNT" "20" "MATCH" "Test:*"
1603241935.282568 [0 127.0.0.1:52536] "SCAN" "41" "COUNT" "20" "MATCH" "Test:*"
1603241935.284090 [0 127.0.0.1:52536] "SCAN" "185" "COUNT" "20" "MATCH" "Test:*"
1603241935.285949 [0 127.0.0.1:52536] "SCAN" "117" "COUNT" "20" "MATCH" "Test:*"
1603241935.288443 [0 127.0.0.1:52536] "SCAN" "253" "COUNT" "20" "MATCH" "Test:*"
1603241935.293034 [0 127.0.0.1:52536] "SCAN" "147" "COUNT" "20" "MATCH" "Test:*"
1603241935.294957 [0 127.0.0.1:52536] "SCAN" "155" "COUNT" "20" "MATCH" "Test:*"
1603241935.296113 [0 127.0.0.1:52536] "SCAN" "199" "COUNT" "20" "MATCH" "Test:*"
1603241935.296456 [0 127.0.0.1:52536] "SCAN" "247" "COUNT" "20" "MATCH" "Test:*"
1603241935.296723 [0 127.0.0.1:52536] "SCAN" "223" "COUNT" "20" "MATCH" "Test:*"

相关问题