如何在Predis中将SCAN与MATCH选项一起使用 [英] How to use SCAN with the MATCH option in Predis

查看:149
本文介绍了如何在Predis中将SCAN与MATCH选项一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾使用 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 for SCAN
  • HashKey for HSCAN
  • SetKey for SSCAN
  • SortedSetKey for ZSCAN
  • ListKey for LRANGE - This doesn't really use Redis iterators, but it's a nice interface to LRANGE anyway.

这篇关于如何在Predis中将SCAN与MATCH选项一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆