15

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);
1
  • 1
    You want to iterate through the array elements and insert them individually. You can write a simple for loop or use Array.prototype.forEach. Commented Nov 2, 2013 at 18:46

3 Answers 3

24

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Using redis-node that wasn't working for me, slightly modified here: gist.github.com/vainrobot/141b2162e1200f7c7855
9

Even if @gimenete answer works, the best way to do what you want is to forward the list elements as arguments to rpush like so:

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

var arr = [1,2,3];
client.rpush.apply(client, ['testlist'].concat(arr));

// ... or with a callback
client.rpush.apply(client, ['testlist'].concat(arr).concat(function(err, ok){
  console.log(err, ok);
}));

Pros: - a single instruction will be transmited to Redis

Cons: - a corner-case: .apply will throw a RangeError: Maximum call stack size exceeded if the arguments list length passed to rpush is too large (a little over 100 000 items for v8).

From MDC:

The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function.

3 Comments

Not work for me. Error: ERR wrong number of arguments for 'rpush' command.
Exactly as you type . There is no other code in my node js file.
Sorry, that is the problem with my redis server. I use it on window and can't execute RPUSH with multiple value.
0
import { createClient } from "redis";

const redisClient = createClient();

const arr = [1, 2, 3];

async function cacheList() {
  await Promise.all(arr.map((elem) => redisClient.rPush("testlist", elem)));
}

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has a couple of answers—including one that has been extensively validated by the community. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.