开发者

How should I store this object in Redis?

开发者 https://www.devze.com 2023-04-05 03:24 出处:网络
parent = { child0: { data1:\'foo\', data2: \'bar\' }, child1: { data1:\'foo\', data2: \'bar\' }, child2: { data1:\'foo\',
parent = {
    child0: {
        data1:'foo',
        data2: 'bar'
    },
    child1: {
        data1:'foo',
        data2: 'bar'
    },
    child2: {
        data1:'foo',
        data2: 'bar'   
    }
}

At first I was thinking I would set a parent:child key since I will need that data individually of its siblings. In some instances though, I will need to return all of the data within parent.

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

Are there downsides to this if a lot of gets and 开发者_如何学编程sets may only be for one of it's children?

Is there a way to call all parent data with a parent:child schema?

Thanks!


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

Storing the whole object as JSON in a single key is also valid though, and keeps your code simple if your usage is a good fit. If the numbers aren't too large, it may make sense to always retrieve the whole object even when you only need to display one child object.

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


You might consider taking advantage of the hash data type. Using the parent as a key to the hash and using (HGET key field) for a particular child or (HKEYS key) for all the children.

It would be interesting if someone would post benchmarks for hash commands HSET and HGET. The benchmarks for the list operations (LPUSH 88109.25 /sec) are (~23%) slower though (SET 114293.71 /sec). Presumably HSET is yet slower though listed O(1).

So I think you would speed optimize the decision by looking at the ratio of full family requests to individual child request in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号