问题
I am trying to use redis scan with laravel. I can make a single request which returns 10 keys but I wish to loop until all the keys have been returned. I am unsure how to do this with laravel. Currently I have
$test = Redis::scan(0, 'match', '*keypattern*');
I don't know if there is a 'laravel' way of doing this.
EDIT:
I used composer to import predis/predis and got it working with
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;
}
but I would like to know the laravel way
EDIT:
var_dump of the single Redis::scan
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"
}
}
回答1:
As the Redis facade passes commands directly to Predis (or Redis itself you might say), this goes hand in hand with the Redis docs (http://redis.io/commands/scan). You can use the cursor (first array entry) for subsequent calls to iterate until the cursor is zero.
I've put together a recursive approach, to scan all entries:
function scanAllForMatch ($pattern, $cursor=null, $allResults=array()) {
// Zero means full iteration
if ($cursor==="0") {
return $allResults;
}
// No $cursor means init
if ($cursor===null) {
$cursor = "0";
}
// The call
$result = Redis::scan($cursor, 'match', $pattern);
// Append results to array
$allResults = array_merge($allResults, $result[1]);
// Recursive call until cursor is 0
return scanAllForMatch($pattern, $result[0], $allResults);
}
Note that you might need to add $this before the recursion, if you use this in a class (would be return $this->scanAllForMatch(...))
You would call it like that:
// Don't pass a cursor yourself!
$allResults = scanAllForMatch('*keypattern*');
回答2:
Thanks @martinczerwi Here is a non-recursional version:
function scanAllForMatch($pattern)
{
$cursor = 0;
do {
list($cursor, $keys) = Redis::scan($cursor, 'match', $pattern);
foreach ($keys as $key) {
yield $key;
}
} while ($cursor);
}
来源:https://stackoverflow.com/questions/35477172/laravel-and-redis-scan