javascriptnode.jsredisnode-redis

How to save javascript array as redis list


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);

Solution

  • 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.