问题描述
我想从散列中获取所有键及其值,其中键匹配特定模式.我将 redis 与 c# 库 ServiceStack.Redis 一起使用.
I would like to get all keys, with its values, from a hash, where the keys match a specific pattern. I use redis with the c# library ServiceStack.Redis.
我找到了命令 Keys with a pattern
:http://redis.io/commands/keys 如果它是简单的 string_key -- string_val 但如果它在散列中则什么都没有.
I have found the command Keys with a pattern
: http://redis.io/commands/keys if it is simple string_key -- string_val but nothing if its within a hash.
有ListGetValuesFromHash(string hashId, params string[] keys);
但它仅在键与 redis 中的键完美匹配时才有效.key:*
将返回 null
There is List<string> GetValuesFromHash(string hashId, params string[] keys);
but it only works if the keys perfectyl match the keys in redis. A key:*
would return null
例如
myHash =
key:1 -- val1,
asdf -- asdfe,
key:2 -- val2
现在我想从 myHash
中获取所有带有其值的键,如果哈希中的键与以下模式匹配:key:*
Now I would like to get all keys with its values from myHash
if the key, within the hash, matches the following pattern: key:*
这将导致
key:1 -- val1,
key:2 -- val2
推荐答案
Redis 不直接支持:http://redis.io/commands#hash
Redis does not support this directly: http://redis.io/commands#hash
您仅限于一次查询所有键或按其确切名称指定的一个或多个键.这种使用模式可能意味着您需要一个散列加上另一个数据结构(例如集合)来记录有趣的键,或者两个或多个单独的散列.由于 Redis 支持一次对多个结构进行原子更新,因此这通常是可行的方法.
You are limited to querying all keys at once or one or more keys specified by their exact name. This usage pattern probably means you need a hash plus another data structure (e.g. set) to keep record of the interesting keys, or two or more separate hashes. Since Redis supports atomic updates to multiple structures at once this is usually the way to go.
这篇关于如何从redis中的散列中获取与特定模式匹配的所有键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!