Last active
May 23, 2020 15:58
-
-
Save dpaluy/bdd780e8835546da03c6 to your computer and use it in GitHub Desktop.
Pipeling Multiple REDIS Commands in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pipeliner.pipeline redis do |pipe| | |
objects.each do |object| | |
pipe.enqueue redis.hgetall(object.key) do |result| | |
object.values = result | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pipeliner | |
def initialize(redis) | |
@redis = redis | |
@cmds = [] | |
end | |
def enqueue(future, &proc) | |
@cmds << { future: future, callback: proc } | |
end | |
def wait | |
@cmds.each do |c| | |
while c[:future].value.is_a?(Redis::FutureNotReady) | |
sleep(1.0 / 100.0) | |
end | |
c[:callback].call c[:future].value | |
end | |
end | |
def self.pipeline(redis, &proc) | |
# Executes callbacks with each result. This blocks. | |
pipeliner = Pipeliner.new redis | |
redis.pipelined do | |
proc.call pipeliner | |
end | |
pipeliner.wait | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source: http://mrdanadams.com/2012/pipeline-redis-commands-ruby/