I am attempting to push multiple values to a redis LIST using LPUSH. The code looks something like this:
mylist = ["1", "2", "3", "4"]
$redis.lpush(name, mylist)
The problem with the above is the list gets flattened to look like "1234". How do I use LPUSH in this case to push 4 individual elements to the name
array?
You may want to have a look at:
https://github.com/redis/redis-rb/blob/master/CHANGELOG.md
https://github.com/redis/redis-rb/issues/220
https://github.com/redis/redis-rb/pull/221
Redis-rb version 2.2.2 does not support variadic commands. They have been introduced for version 3.0, but it has not been delivered yet.
There is no specific method to deal with arrays in variadic parameters commands. Your code will probably work fine with version 3.0.
In the meantime, I suggest you use a pipeline such as:
name = "toto"
mylist = [ 1,2,3,4,5 ]
$redis.pipelined{ mylist.each{ |x| $redis.lpush(name,x) } }
With the pipeline, the number of roundtrips will be the same than using variadic parameters commands.
If you really want to use variadic parameters commands, you can easily install a prerelease version of 3.0 using:
gem install --prerelease redis
gem list redis
*** LOCAL GEMS ***
redis (3.0.0.rc1, 2.2.2)
and activate it in your script by using:
gem 'redis', '3.0.0.rc1'
require 'redis'
Please note that if you have other gems depending on the redis gem, you will have some dependency issues. Better to wait for the official delivery IMO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With