我有几个关键模式
redis-ruby-gem版本4.2.5
app:namespace1:{id}
app:namespace2:{id}
app:namespace1:{id}:nested1
app:namespace2:{id}:nested2
app:whatever1
app:whatever2
如何扫描所有app:*
?
到目前为止,如果我可以为app:*
,我只得到顶级的非嵌套值,比如
app:whatever1
app:whatever2
def match_keys(pattern)
@redis ||= init_connection
all_keys = []
cursor = 0
loop do
cursor, keys = @redis.scan(cursor, match: pattern)
all_keys += keys
break if cursor == '0'
end
all_keys.uniq
end
match_keys 'app:*'
#=> ["app:whatever1", "app:whatever2"]
我需要它返回整个app:*
命名空间密钥,包括所有子密钥
发现
看起来我的问题与搜索字符串无关,因为我的redis环境在集群环境中运行,所以密钥分布在多个集群中,集群用自己节点上的密钥进行响应,而不是从其他
那么,我如何让我的redisruby集群扫描集群中所有匹配的密钥呢?
这是我的集群配置
def init_connection
config = {
cluster: [
"redis://#{ENV['REDIS_HOST_1']}",
"redis://#{ENV['REDIS_HOST_2']}",
"redis://#{ENV['REDIS_HOST_3']}"
],
replica: true
}
@redis ||= ::Redis.new(config)
end
根据这篇文章,如何在集群环境中使用redis扫描?
我似乎需要找到一种在每个节点上循环的方法,但我找不到任何文档来完成
您是否考虑升级redis-rb
?
在最新版本中,集群支持被转移到了redis-clustering
gem中,后者又在后台使用redis-cluster-client
。
后者似乎在所有节点上执行扫描,因此很有可能使用最新的redis-rb,您不需要手动升起太阳。