简体   繁体   English

php-redis (new Redis()) getKeys() 方法是否使用“KEYS *”或“SCAN”进行迭代?

[英]php-redis (new Redis()) getKeys() method is using "KEYS *" or "SCAN" with iterations?

I am using the php-redis packages mostly available in most popular repos.我使用的 php-redis 包大多在最流行的存储库中可用。 The one you call as你称之为的那个

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

Here if I do如果我这样做

$var = $redis->getKeys('something.*');

On the backend will it do the sync and blocking在后端,它会进行同步和阻塞吗

KEYS something.*

Or will use scan iteratively non-blockingly allowing concurrent threads to work as或者将使用扫描迭代非阻塞允许并发线程作为

SCAN 0 MATCH something.* COUNT 10
SCAN $iteratorFromLastCall MATCH something.* COUNT 10
... 
while ($iteratorFromLastCall > 0);

Or something similar?或者类似的东西?

As the former is not supposed to be used in production and shows no issues while developing, the latter is "less guaranteed" though, but much more reliable for a high availability requirement.由于前者不应该在生产中使用并且在开发过程中没有显示任何问题,因此后者虽然“不太有保证”,但对于高可用性要求更可靠。

Do I need to use我是否需要使用

$redis->scan()

And iterate the cursor myself, or并自己迭代光标,或者

$redis->getKeys()

Is already "production safe" avoiding "KEYS *"?已经“生产安全”避免“KEYS *”了吗?

You can see in the source that getKeys() uses the KEYS command:源码中可以看到getKeys()使用了KEYS命令:

/* {{{ proto array Redis::getKeys(string pattern)
 */
PHP_METHOD(Redis, getKeys)
{
    REDIS_PROCESS_KW_CMD("KEYS", redis_key_cmd, redis_mbulk_reply_raw);
}
/* }}} */

So you'll need to go with your second option, using scan() and iterating.因此,您需要使用第二个选项,使用scan()和迭代。 As you say, KEYS is not recommended for production environments:如您所说,生产环境不建议使用KEYS

Warning: consider KEYS as a command that should only be used in production environments with extreme care.警告:将 KEYS 视为仅应在生产环境中极其小心地使用的命令。 It may ruin performance when it is executed against large databases.当它针对大型数据库执行时,它可能会破坏性能。 This command is intended for debugging and special operations, such as changing your keyspace layout.此命令用于调试和特殊操作,例如更改键空间布局。 Don't use KEYS in your regular application code.不要在常规应用程序代码中使用 KEYS。 If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.如果您正在寻找一种在键空间子集中查找键的方法,请考虑使用 SCAN 或集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM