How to save this object in Redis?

parent = { child0: { data1:'foo', data2: 'bar' }, child1: { data1:'foo', data2: 'bar' }, child2: { data1:'foo', data2: 'bar' } } 

At first, I thought I set the parent:child key, since I will need this data separately from his siblings. However, in some cases, I will need to return all the data inside the parent.

Should I just put the whole object in the parent key?

Are there any drawbacks in this case, if many sets and sets can be for only one of them?

Is there a way to invoke all parent data using the parent:child schema?

Thanks!

+4
source share
2 answers

Try a hash - this gives you HGET to get only one child and HGETALL to get all of them.

Saving the whole object as JSON in one key is also valid, although it keeps your code simple if your use is appropriate. If the numbers are not too large, it may make sense to always retrieve the entire object, even if you only need to display one child object.

The main reason to avoid storing complex objects in one key is write conflicts - if two connections can modify different child objects of the same object, at the same time, the hash will be much less problem.

+2
source

You may consider using hash data. Using a parent as a key for a hash and using (HGET key field) for a specific child or (HKEYS key) for all children.

It would be interesting if someone sent checkmarks for the hash commands HSET and HGET. Benchmarks for list operations (LPUSH 88109.25 / sec) are slower (~ 23%) (SET 114293.71 / sec). HSET is supposed to be even slower than O (1) indicated.

Therefore, I think that you would quickly optimize the solution by looking at the ratio of the full family request to the individual child request in your code.

0
source

All Articles