Redis-rb, pushing an array through redis.lpush, smoothes the list

I am trying to push multiple values ​​in redis LIST using LPUSH. The code looks something like this:

mylist = ["1", "2", "3", "4"]
$redis.lpush(name, mylist)

The problem with the above list is smoothed out to look like "1234". How can I use LPUSH in this case to insert 4 separate elements into the name array?

+6
source share
3 answers

You can watch:

Version Redis-rb 2.2.2 does not support command variables. They were introduced for version 3.0, but it has not yet been delivered.

There is no special method for processing arrays in variable parameter commands. Your code probably works great with version 3.0.

In the meantime, I suggest you use a pipeline, for example:

 name = "toto" mylist = [ 1,2,3,4,5 ] $redis.pipelined{ mylist.each{ |x| $redis.lpush(name,x) } } 

With the pipeline, the number of round-trip calls will be the same as when using variable parameter commands.

If you really want to use variable parameter commands, you can easily install the preliminary version 3.0 using:

 gem install --prerelease redis gem list redis *** LOCAL GEMS *** redis (3.0.0.rc1, 2.2.2) 

and activate it in the script using:

 gem 'redis', '3.0.0.rc1' require 'redis' 

Please note that if you have other gems depending on redis pearls, you will have addiction problems. Better to wait for the official delivery of IMO.

+2
source

You can do:

 # If you want ["1","2","3","4",...restofarray...] mylist.reverse.each{ |v| $redis.lpush(name,v) } # If you want ["4","3","2","1",...restofarray...] mylist.each{ |v| $redis.lpush(name,v) } 
0
source

C v 3.0 + syntax

 r= Redis.new() r.lpush("mylist", ["element1", "element2" , "element3"]) 
0
source

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


All Articles