Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How should I store this object in Redis?

How should I store this object in Redis?

Problem

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!

Problem courtesy of: fancy

Solution

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.

Solution courtesy of: Tom Clarkson

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How should I store this object in Redis?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×