php - 无法将 'KEYS' 与 redis-cluster 一起使用

标签 php caching redis redis-cluster predis

我最近正在开发的一个应用程序从 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));
    }
}

现在返回上述错误。有没有其他方法可以获取此指定命名空间下的所有数据?

最佳答案

来自官方Redis documentation

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/

相关文章:

ruby - Redis:我应该将 IP 转换为整数吗?

php - 如何使用官方 PHP Docker 镜像方法安装 php-redis 扩展?

php - 来自电子邮件地址的低级安全散列

php - 被拒绝 Apache2

ruby-on-rails - 如何在 Rails 3.1 中获取片段到期日期?

lua - 使用 Lua 脚本的多个 HMSET 到 Redis

phpUnit fatal error

javascript - Symfony2在组件中找不到jquery.js文件

javascript - 如何将版本号添加到 HTML 文件(不仅是 css 和 js 文件)

caching - ARM Cortex-M4 中是否有缓存?