Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Reverse Lookup in Redis?

Tags:

redis

nosql

Is it possible to use Redis to lookup by key OR value? I need to store a master list of email addresses, assign a UUID to each address, but be able to find the ID OR ADDRESS by using the other piece of data. I cannot find a definitive "yes" or "no." Any examples would be appreciated.

like image 973
Ryan D'Baisse Avatar asked Feb 13 '14 23:02

Ryan D'Baisse


1 Answers

You'll want to create 2 hashmaps:

  1. Hashmap from UUID to email (uuid_to_email)
  2. Hashmap from email to UUID (email_to_uuid)

For example:

> hset uuid_to_email 39315120-9581-11e3-9c4e-0002a5d5c51b [email protected]
> hset email_to_uuid [email protected] 39315120-9581-11e3-9c4e-0002a5d5c51b

Then to retrieve the value, use the hashmap that maps from the value you have. If you have a UUID, use uuid_to_email:

> hget uuid_to_email 39315120-9581-11e3-9c4e-0002a5d5c51b
"[email protected]"

If you have an email, use email_to_uuid:

> hget email_to_uuid [email protected]
"39315120-9581-11e3-9c4e-0002a5d5c51b"
like image 121
uberdog Avatar answered Sep 23 '22 19:09

uberdog