How to get all keys that match a specific pattern from a hash in redis?

依然范特西╮ 提交于 2019-12-12 11:32:39

问题


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.

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.

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

e.g.

myHash = 
key:1 -- val1, 
asdf -- asdfe,
key:2 -- val2

Now I would like to get all keys with its values from myHash if the key, within the hash, matches the following pattern: key:*

That would result in

key:1 -- val1, 
key:2 -- val2

回答1:


Redis does not support this directly: http://redis.io/commands#hash

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.



来源:https://stackoverflow.com/questions/19404847/how-to-get-all-keys-that-match-a-specific-pattern-from-a-hash-in-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!