How to store array of objects in Redis?
Asked Answered
D

4

7

I have an array of Objects that I want to store in Redis. I can break up the array part and store them as objects but I am not getting how I can get somethings like

{0} : {"foo" :"bar", "qux" : "doe"}, {1} : {"name" "Saras", "age" : 23} 

and then search the db based on name and get the requested key back. I need something like this. but can't come close to getting it right.

incr id //correct
(integer) 3
get id //correct
"3"
SADD id {"name" : "Saras"} //wrong 
SADD myset {"name" : "Saras"} //correct
(integer) 1

First is getting this part right.

Second is somehow getting the key from the value i.e.

if name==="Saras"  
then key=1

Which I find tough. Or I can store it directly as array of objects and use a simple for loop.

 for (var i = 0; i < userCache.users.length; i++) {
            if (userCache.users[i].userId == userId && userCache.users[i].deviceId == deviceId) {
                return i;
            }
        }

Kindly suggest which route is best with some implementation?

Dogs answered 3/8, 2016 at 15:6 Comment(1)
You could store an array of unique ids, each one the key of a seperately stored hash.Lissotrichous
D
9

The thing I found working was storing the key as a unique identifier and stringifying the whole object while storing the data and applying JSON.parse while extracting it.

Example code:

client
    .setAsync(obj.deviceId.toString(), JSON.stringify(obj))
    .then((doc) => {
        return client.getAsync(obj.deviceId.toString());
    })
    .then((doc) => {
        return JSON.parse(doc);
    }).catch((err) => {
        return err;
    });

Though stringifying and then parsing it back is a computationally heavy operation and will block the Node.js server if the size of JSON becomes large. I am probably ready to take a hit for lesser complexity because I know my JSON wouldn't be huge, but that needs to be kept in mind while going for this approach.

Dogs answered 15/4, 2017 at 11:52 Comment(1)
Thanks for verifying this as a solution I was thinking to do the same thingTelestich
W
2

Redis is pretty simple key-value storage. Yes, there are other data structures like sets, but it has VERY limited query capabilities. For example, if you want to get find data by name, then you would have to to something like that:

SET Name "serialized data of object"

SET Name2 "serialized data of object2"

SET Name3 "serialized data of object3"

then:

GET Name

would return data.

Of course this means that you can't store two entries with the same names.

You can do limited text matching on keys using: http://redis.io/commands/scan

To summarize: I think you should use other tool for complex queries.

Wylma answered 10/8, 2016 at 7:44 Comment(2)
Hey @kiss-web what other technologies I can use?Dogs
Non-relational DB which allows queries on its contents, like: pl.wikipedia.org/wiki/MongoDB (native support for json).Wylma
G
0

The first issue you have, SADD id {"name" : "Saras"} //wrong, is obvious since the "id" key is not of type set, it is a string type.

In redis the only access point to data is through its key. As kiss said, perhaps you should be looking for other tools.

Ge answered 11/8, 2016 at 8:53 Comment(1)
By other tools what do you mean? Can you give some example?Dogs
G
0

If you were trying to store a array of object which is in the form of [ {}, {}, {} ]

Just set the data as it is in the form and while retrieving it just parse it I hope this will work.

Groat answered 17/10, 2023 at 9:10 Comment(1)
The original poster's (OP) question was more nuanced than that.Brandwein

© 2022 - 2024 — McMap. All rights reserved.