Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis.lpush a number of items

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

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

which results in a single string being pushed:

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

Redis supports multiple values in LPUSH command, I am looking for help on utilizing this functionality. I am not asking how to loop over my array and push each item 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 run time, and I do not know its contents.

like image 567
akonsu Avatar asked Aug 07 '13 04:08

akonsu


People also ask

How to use lpush in Redis?

Redis LPUSH command inserts all the specified values at the head of the list stored at the key. If the key does not exist, it is created as an empty list before performing the push operations. When the key holds a value that is not a list, an error is returned.

How do I list in Redis?

Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush command or the tail (the “right” end) with rpush . You can also use lpush or rpush to create a new list: lpush key value.

How do I display a list in Redis?

Redis Get List Items. To get elements in a Redis, use the LRANGE command. This command takes the name of the list and the index range of the element you wish to access. The command should return the values of the elements in the specified range.

How do I clear a list in Redis?

Delete the key, and that will clear all items. Not having the list at all is similar to not having any items in it. Redis will not throw any exceptions when you try to access a non-existent key.


1 Answers

This is now possible with es6 spread syntax. An array of any size will be spread out as the arguments to the function.

redis.lpush('queue', ...arrayOfValues)
like image 155
SofieGraham Avatar answered Oct 26 '22 15:10

SofieGraham