tofutim tofutim - 16 days ago 5
Node.js Question

How should I store JSON in redis?

I have JSON (<1k) to store in Redis through node.js. What are the pros and cons of storing it as an object or string? Are there other options I missed? All processing will ultimately happen on the client side, so converting into an object is not necessary.

SET

var images = JSON.parse(data); // data is already JSON, is this needed?
callback(images); // sends result to the user
r.set('images:' + req.query, images); // saving the object


GET

callback(images);

Answer

You can store JSON in redis either as a plain string in dedicated key (or member/value of a set/list) or in a hash structure. If you look at node_redis docs into Friendlier hash commands part you'll see that it gives you some useful methods for manipulating JSON based data. Pros of this approach is that it allows you to get/set only part of the original object and it might also consume less memory compared to plain strings.