Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Last active May 23, 2020 15:58
Show Gist options
  • Save dpaluy/bdd780e8835546da03c6 to your computer and use it in GitHub Desktop.
Save dpaluy/bdd780e8835546da03c6 to your computer and use it in GitHub Desktop.
Pipeling Multiple REDIS Commands in Ruby
Pipeliner.pipeline redis do |pipe|
objects.each do |object|
pipe.enqueue redis.hgetall(object.key) do |result|
object.values = result
end
end
end
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
@dpaluy
Copy link
Author

dpaluy commented Nov 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment