Redis扫描跳过键 [英] Redis scan skipping keys

查看:140
本文介绍了Redis扫描跳过键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用predis(如果有任何区别,请与laravel一起使用)php客户端与Redis一起使用.

I'm using predis (with laravel if it makes any difference) php client to work with Redis.

我需要从Redis中获取与某些前缀匹配的所有键,我这样做是这样的:

I need to fetch all the keys from Redis that match certain prefix and I do it like this:

$keys = [];
    foreach (new Iterator\Keyspace($this->redis(), Cache::KEY_PREFIX.'*') as $key) {
        $keys[] = $rate_key;
    }

使用这些键完成操作后,将重复进行操作-我再次将这些键重新放入一个循环中. 我注意到经过几次迭代后,某些键未包含在$ keys数组中.

After the work with those keys is done, operation repeats - I'm getting those keys again in again in a loop. I noticed that after a few iterations some keys are not included in $keys array.

最奇怪的是消失的键永远不会出现在下一个迭代中.重新启动php进程(这是一个守护程序)可以解决此问题.

The strangest thing is that keys that disappear never appear in next iterations. Restart of the php process (it's a daemon) fixes the problem.

我正在将Redis 3.0.2与Predis 1.0和PHP 5.4一起使用

I'm using Redis 3.0.2 with Predis 1.0 and PHP 5.4

P.S.在循环键中,我更改了其中一些的值.但是,我没有删除任何键.

P.S. Within the loop over keys, I change values for some of them. I'm not deleting any keys, however.

推荐答案

确实!这是因为SCAN以这种方式工作,引用了Redis文档:

Indeed! That's because SCAN works that way, quoting from Redis documentation:

但是,尽管像SMEMBERS这样的阻塞命令能够在给定的时间内提供Set中所有元素,但 SCAN系列命令仅对返回的元素提供有限保证,因为该集合已被收集在迭代过程中,我们进行增量迭代的过程可能会发生变化.

However while blocking commands like SMEMBERS are able to provide all the elements that are part of a Set in a given moment, The SCAN family of commands only offer limited guarantees about the returned elements since the collection that we incrementally iterate can change during the iteration process.

但是,由于SCAN具有很少的关联状态(仅是游标),因此具有以下缺点: 给定的元素可能会多次返回.由应用程序来处理重复元素的情况,例如仅使用返回的元素来执行多次重新应用时安全的操作.

However because SCAN has very little state associated (just the cursor) it has the following drawbacks: A given element may be returned multiple times. It is up to the application to handle the case of duplicated elements, for example only using the returned elements in order to perform operations that are safe when re-applied multiple times.

因此,您可能想使用 array_unique($键) .

So you may want to use want to use array_unique($keys) after the foreach.

要了解为什么迭代如此工作,最好的方法是阅读 Redis文档的这一部分 .

To understand why the iteration works that way the best thing is to read this part of the Redis documentation.

这篇关于Redis扫描跳过键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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