How to reverse search in redis?

Can I use Redis to search by the OR key? I need to keep the main list of email addresses, assign a UUID to each address, but be able to find the ID OR ADDRESS using another piece of data. I cannot find the final yes or no. Any examples would be appreciated.

+3
source share
3 answers

You need to create 2 hashmaps:

  • Hashmap with UUID to email address (uuid_to_email)
  • Hashmap from email to UUID (email_to_uuid)

For instance:

> hset uuid_to_email 39315120-9581-11e3-9c4e-0002a5d5c51b foo@bar.com
> hset email_to_uuid foo@bar.com 39315120-9581-11e3-9c4e-0002a5d5c51b

Then, to get the value, use a hash map that displays the value that you have. If you have a UUID, use uuid_to_email:

> hget uuid_to_email 39315120-9581-11e3-9c4e-0002a5d5c51b
"foo@bar.com"

If you have an email, use email_to_uuid:

> hget email_to_uuid foo@bar.com
"39315120-9581-11e3-9c4e-0002a5d5c51b"
+2

,

redis 127.0.0.1:6379> SET XXX:abc@yahoo.com XXX:abc@yahoo.com
OK
redis 127.0.0.1:6379> SET YYY:xyz@gmail.com YYY:xyz@gmail.com
OK
redis 127.0.0.1:6379> keys YYY:*
1) "YYY:xyz@gmail.com"
redis 127.0.0.1:6379> keys *:xyz@gmail.com
1) "YYY:xyz@gmail.com"
redis 127.0.0.1:6379> keys XXX:*
1) "XXX:abc@yahoo.com"
redis 127.0.0.1:6379> keys *:abc@yahoo.com
1) "XXX:abc@yahoo.com"

DRAWBACK:: , , .

: -: , O (1),

redis 127.0.0.1:6379> SET XXX abc@gmail.com
OK
redis 127.0.0.1:6379> SET abc@gmail.com XXX
OK
redis 127.0.0.1:6379> SET YYY xyz@gmail.com
OK
redis 127.0.0.1:6379> SET xyz@gmail.com YYY
OK
redis 127.0.0.1:6379> GET XXX 
"abc@gmail.com"
redis 127.0.0.1:6379> GET abc@gmail.com
"XXX"
redis 127.0.0.1:6379> GET YYY
"xyz@gmail.com"
redis 127.0.0.1:6379> GET xyz@gmail.com
"YYY"

DRAWBACK:

+2

- redis. , - , .

, . , " ?":

incr: ( ) 1

hmset user: 1 lloyd uk OK

sadd user: name: lloyd 1 ( ) 1

sadd user: likes: whisky 1 ( ) 1

sadd user: lives: uk 1 ( ) 1

incr: ( ) 2

hmset user: 2 ivan uk OK

sadd user: name: ivan 2 ( ) 1

sadd user: like: whisky 2 ( ) 1

sadd user: likes: whiskey 2 ( ) 1

sadd user: lives: nyc 2 ( ) 1

hmset user: 2 ivan nyc OK

hget: 2 name ""

sunion : : whiskey user: lives: uk 1) "1" 2) "2"

sinter user: likes: whiskey user: lives: uk 1) "1"

sdiff : likes: whiskey user: lives: uk 1) "2"

: : whiskey - * GET : * → name 1) "lloyd" 2) "ivan"

sort user: like: whiskey by weight - * GET user: → name GET user: → lives 1) "lloyd" 2) "uk" 3) "ivan" 4) "nyc"

0
source

All Articles