Basit Anwer
Basit Anwer

Reputation: 6870

Store two sets of tables in redis

Using Python and redis api;

I want to store a Dictionary such as

Value -> List of Words(L.O.W)
Each word in L.O.W -> Value

Basically i want everything to be searchable (in the best possible manner) to and fro but since redis doesn't support multiple table/records, how would we handle this in redis ?

PS: I'm considering redis is the best choice to do so and i'm new to python as well

Upvotes: 1

Views: 1652

Answers (2)

rchang
rchang

Reputation: 5236

For the value->L.O.W., I think there are two viable options:

1) Store the L.O.W. as a set with the value as the key name. In redis-py this may look something like this:

import redis
db = redis.StrictRedis() # Put connection info here
...
...
db.sadd(value, *listOfWords) # Assuming listOfWords is an iterable object (list, set, etc.)

#To recover the LOW for a given value:
listOfWords = db.smembers(value)

2) Store all value->L.O.W. lookups as a hash whose key/value pairs are your "value" and a "serialized" list of words respectively. The top-level hash would be given a key name to make sure its function is obvious (in the example below I chose value_low_lookup:

db.hset(`value_low_lookup`, value, ';'.join(listOfWords))

#Recovering the LOW
listOfWords = db.hget('value_low_lookup', value).split(';')

Option #2 is more memory efficient in terms of storage in Redis, but requires more processing on the client side (pull out the semicolon-concatenated string and separate it into the list of words). You would also have to perform uniqueness-guarantees (if that's a requirement) client-side prior to insertion into Redis.

For the L.O.W.->value, I think a hash may be the most efficient way to represent that in Redis, similar to option #2 above. The key/value pairs in this hash could be a word and the value associated to the LOW from which the word came, respectively.

Upvotes: 0

Itamar Haber
Itamar Haber

Reputation: 49962

The straightforward approach is to use two types of Sets: one for storing the value->LOW and once for the opposite direction, word from LOW->values.

SADD val1 low1 low2
SADD val2 low2 low3
SADD low1 val1
SADD low2 val1 val2
SADD low3 val2

Upvotes: 1

Related Questions