我最近正在开发的一个应用程序从 redis
迁移到了 redis cluster
。现在我一直在使用 keys
命令来获取键空间下的所有键。但现在它说 Cannot use 'KEYS' with redis-cluster
。我用 Laravel 编写的应用程序使用 Predis
作为 redis 客户端。
我在 redis 中存储了一个树响应,它位于 parent:child:ItemID
格式的命名空间下。所以我有像
parent:child:1
parent:child:2
parent:child:3
并且这些键中的每一个都有与之关联的 JSON。我正在获取循环遍历键的每个 JSON 条目。代码如下:
$keys = $this->redis->keys('parent:child:*');
if (!empty($keys)) {
foreach ($keys as $key) {
$cacheData[] = json_decode($this->redis->get($key));
}
}
现在返回上述错误。有没有其他方法可以获取此指定命名空间下的所有数据?
最佳答案
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.
如果您使用的是predis
,您可以像这样example :
foreach (new Iterator\Keyspace($this->redis, 'parent:child:*') as $key) {
$cacheData[] = json_decode($this->redis->get($key));
}
关于php - 无法将 'KEYS' 与 redis-cluster 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57069752/