bellyflop
bellyflop

Reputation: 155

How to store list of dictionaries returned in Redis using NodeJS?

I am writing a NodeJS app that does a get request to a third party api. This api returns a list of dictionaries. I would like to take this result, and store it in my redis client. How do I do that?

The results object looks like this -

[{"foo":123, "bar":456}, {"foo":789, "bar":012}]

Upvotes: 0

Views: 614

Answers (2)

Sohan
Sohan

Reputation: 6809

I would do it simple assuming your data as list or list as string,

      let redis = require("redis"),
      publisher = redis.createClient(option.redisPort, option.redisHost); 
      publisher.publish("id", DATA);  // here we publish the data. 

Are you looking for something like this?

Upvotes: 0

generalhenry
generalhenry

Reputation: 17319

It sounds like you don't need to query elements from the dicts so it should be fine just storing them as json strings in a redis list. When you bulk add to the list you can stringify them, just make sure to use some form of pipe-lining so you're not making unneeded api calls. Then you can simply parse the json string dicts when you need them. ex:

function setList (list, callback) {
  const pipeline = redis.pipeline();
  for (const dict of list) {
    pipeline.rpush('myList', JSON.stringify(dict));
  }
  pipeline.exec(callback);
}

function popList (callback) {
  redis.lpop('mylist', function (error, data) {
    if (error) { return callback(error) }
    callback(null, JSON.parse(data));
  });
}

Upvotes: 1

Related Questions