简体   繁体   中英

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. 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 *"?

You can see in the source that getKeys() uses the KEYS command:

/* {{{ 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. As you say, KEYS is not recommended for production environments:

Warning: consider KEYS as a command that should only be used in production environments with extreme care. 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. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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