如何在Predis中将SCAN与MATCH选项一起使用 [英] How to use SCAN with the MATCH option in Predis
问题描述
我以前曾使用 KEYS
命令在Redis数据库中搜索与特定模式匹配的密钥.从Redis 2.8开始, SCAN
命令似乎优于KEYS
,因为它返回迭代器而不是扫描整个对象一次提供键空间.
I have previously used the KEYS
command to search for keys matching a certain pattern in my Redis database. Since Redis 2.8, the SCAN
command seems to be preferred over KEYS
since it returns an iterator instead of scanning through the whole keyspace at once.
我正在使用 Predis > = 0.8.5,它应该支持SCAN
命令的PHP迭代器. Predis没有很多文档,所以我想知道如何将以下KEYS
命令转换为与之对应的SCAN
命令:
I'm using Predis >= 0.8.5 which is supposed to support PHP iterators for the SCAN
command. Predis doesn't have a lot of documentation, so I'm wondering how to translate the following KEYS
command to it's SCAN
counterpart:
$client->keys($pattern)
我尝试了以下操作:
$client->scan('MATCH', $pattern);
可以工作的一种-但它不会返回本机PHP迭代器.使用Predis的内置迭代器支持真的很好.
Which kind of works - but it doesn't return a native PHP iterator. It would be really nice to use Predis' built-in iterator support.
推荐答案
我在要使用SCAN
搜索数据库中的匹配键,只需使用Predis\Collection\Iterator\Keyspace
类:
To use SCAN
to search for matching keys in a database, you simply use the Predis\Collection\Iterator\Keyspace
class:
use Predis\Collection\Iterator;
$client = ...;
$pattern = 'foo*';
foreach (new Iterator\Keyspace($client, $pattern) as $key) {
...
}
显然,Predis在每个返回迭代器的命令的Predis\Collection\Iterator
中都有一个迭代器类:
Apparently Predis has an iterator class in Predis\Collection\Iterator
for each of the commands that return iterators:
-
Keyspace
用于SCAN
-
HashKey
用于HSCAN
-
SetKey
用于SSCAN
-
SortedSetKey
用于ZSCAN
-
ListKey
用于LRANGE
-这并没有真正使用Redis迭代器,但是它是无论如何.
Keyspace
forSCAN
HashKey
forHSCAN
SetKey
forSSCAN
SortedSetKey
forZSCAN
ListKey
forLRANGE
- This doesn't really use Redis iterators, but it's a nice interface toLRANGE
anyway.
这篇关于如何在Predis中将SCAN与MATCH选项一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!