Redis.lpush multiple items

In my node.js script, I have an array of strings, and I want LPUSH these strings in a Redis queue. I tried:

 var redis = require('redis').createClient(); redis.lpush('queue', ['1', '2', '3']) 

which results in a single line being pressed:

 redis 127.0.0.1:6379> lrange queue 0 -1 1) "1,2,3" 

Redis supports multiple values ​​in the LPUSH , I am looking for help using this function. I am not asking how to iterate over my array and push each element separately. :)

EDIT:

I know if I do this:

 redis.lpush('queue', '1', '2', '3') 

I get what I expect, but in my real application, the array is generated at runtime, and I don't know its contents.

+4
source share
2 answers

This is apparently a known question / general request

One suggested workaround is to use send_command directly

You can also try this (have not tried it yourself):

 myvalues.unshift('queue'); redis.lpush.apply(redis, myvalues); 
+7
source

This is now possible using es6 distribution syntax . An array of any size will be distributed as an argument to the function.

 redis.lpush('queue', ...arrayOfValues) 
0
source

Source: https://habr.com/ru/post/1495669/


All Articles