In my Laravel application, I used to use the predis/predis
package to be able to use Redis for caching. Recently as recommended in the docs I have switched to PhpRedis but part of my code that was using SCAN is broken. In predis when I was using scan like this:
Redis::scan(0,'match',$pattern //Test:*);
I used to get a result like this:
[
"253952",
[
"Test::296589",
"Test::299112",
"Test::332487",
"Test::320358",
],
]
in this case 253952 was the next cursor that I could pass to scan again to get the next batch like this Redis::scan(253952,'match',$pattern //Test:*);
and I could run this in a while loop until I reach the end:
$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);
}
Since I switched to Phpredis I can no longer iterate over the list. First of all the syntax seem to be different:
$it=null;
$keys = RedisManager::scan($it,$pattern);
I'm not sure what is the point of passing null as iterator to the first parameter. It also just returns:
[
"Test::296589",
"Test::299112"
]
which is just part of the result. It doesn't give me everything and it doesn't give me the next cursor. Can someone please guide me in the right direction?
I switched to
Phpredis
on my local and made it work with the following snippet. The documentation states that there are different usages of the scan.While debugging or developing it would be useful to use
monitor
command. While that snippet is working it prints something like this;