How to save javascript array as redis list

Thant Sin Aung picture Thant Sin Aung · Nov 2, 2013 · Viewed 17.1k times · Source

The following code save the whole array as single value in redis list. But I want to save array values individually. How can I do it?

P.S So sorry for poor English.

var redis = require('redis'),
    client = redis.createClient();

var arr = [1,2,3];

client.rpush('testlist',arr);

Answer

gimenete picture gimenete · Nov 2, 2013

Use multi() to pipeline multiple commands at once:

var redis = require('redis'),
    client = redis.createClient();

var arr = [1,2,3];

var multi = client.multi()

for (var i=0; i<arr.length; i++) {
    multi.rpush('testlist', arr[i]);
}

multi.exec(function(errors, results) {

})

And finally call exec() to send the commands to redis.