我正在尝试将 redis scan
与 laravel 一起使用。我可以发出一个返回 10 个键的请求,但我希望循环直到返回所有键。我不确定如何用 laravel 做到这一点。目前我有
$test = Redis::scan(0, 'match', '*keypattern*');
我不知道是否有“laravel”方法可以做到这一点。
编辑:
我使用 composer 导入 predis/predis
并让它与
use Predis\Collection\Iterator;
use Predis;
...
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
foreach (new Iterator\Keyspace($client, '*keypattern*') as $key) {
$arr[] = $key;
}
但我想知道 laravel 的方式
编辑:
单个Redis::scan
的var_dump
array(2) {
[0]=>
string(4) "23"
[1]=>
array(10) {
[0]=>
string(19) "key17"
[1]=>
string(19) "key72"
[2]=>
string(76) "key11"
[3]=>
string(19) "key73"
[4]=>
string(19) "key63"
[5]=>
string(19) "key87"
[6]=>
string(19) "key70"
[7]=>
string(19) "key65"
[8]=>
string(19) "key82"
[9]=>
string(19) "key43"
}
}
最佳答案
谢谢@martinczerwi 这是一个非递归版本:
function scanAllForMatch($pattern)
{
$cursor = 0;
do {
list($cursor, $keys) = Redis::scan($cursor, 'match', $pattern);
foreach ($keys as $key) {
yield $key;
}
} while ($cursor);
}
关于Laravel 和 redis 扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477172/