简体   繁体   English

Redis Hash:如何同时查询键和值

[英]Redis Hash: How to Query on both Key and Value

I want to store key-value pairs(T1,T2) in Redis. 我想在Redis中存储键值对(T1,T2)。 Both key and value are unique. 键和值都是唯一的。 I want to be able to query on both key and value, ie HGET(Key) should return corresponding Value and HGET(Value) should return corresponding Key. 我希望能够同时查询键和值,即HGET(Key)应该返回对应的Value和HGET(Value)应该返回对应的Key。

A trivial approach would be to create 2 Hashes in Redis (T1,T2) and (T2,T1) and then query on appropriate Hash. 一种简单的方法是在Redis(T1,T2)和(T2,T1)中创建2个哈希,然后在适当的哈希上查询。 Problem with this approach is that insertion, update or deletion of pairs would need updates in both Hashes. 这种方法的问题在于,对的插入,更新或删除都需要在两个哈希中进行更新。

Is there a better way to serve my requirement... 有没有更好的方法来满足我的要求...

If one of T1, T2 has an integer type you could use a combo like: 如果T1,T2中的一个具有整数类型,则可以使用如下组合:

1->foo
2->bar

ZADD myset 1 foo
ZADD myset 2 bar

ZSCORE myset foo //returns 1.0 in O(n)
ZSCORE myset bar //return 2.0 in O(n)

ZRANGEBYSCORE myset 1 1 //returns "foo" in O(log(N)+M)

source 资源

If this is not the case then it makes sense to maintain 2 separate hashes, preferably within a Lua script 如果不是这种情况,那么最好保留两个单独的哈希值,最好在Lua脚本中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM