gpt4 book ai didi

php - Redis 扫描跳过键

转载 作者:IT王子 更新时间:2023-10-29 06:03:57 25 4
gpt4 key购买 nike

我正在使用 predis(使用 laravel,如果它有任何不同)php 客户端与 Redis 一起工作。

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

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

完成这些键的工作后,操作会重复 - 我在循环中再次获取这些键。我注意到经过几次迭代后,一些键没有包含在 $keys 数组中。

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

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

附言在键的循环中,我更改了其中一些键的值。但是,我不会删除任何 key 。

最佳答案

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

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.

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($keys) foreach 之后。

要理解为什么迭代以这种方式工作,最好的办法是阅读 this part of the Redis documentation .

关于php - Redis 扫描跳过键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com