Class: RedisPipeliner::Pipeliner
- Inherits:
-
Object
- Object
- RedisPipeliner::Pipeliner
- Defined in:
- lib/redis_pipeliner/pipeliner.rb
Overview
Enqueues commands in a pipeline and waits until they are finished. Usage pattern is to call #enqueue with each REDIS future and a block to process it, then call #wait outside the Redis.pipelined call.
Instance Method Summary collapse
-
#commands ⇒ Object
Returns the enqueue REDIS commands.
-
#enqueue(future, &proc) ⇒ Object
(also: #<<)
Adds a command (a Future, actually) with an option block to call when the Future has been realized.
-
#initialize(redis) ⇒ Pipeliner
constructor
A new instance of Pipeliner.
-
#values ⇒ Object
(also: #wait)
Blocks until all Futures have been realized and returns the values.
Constructor Details
#initialize(redis) ⇒ Pipeliner
Returns a new instance of Pipeliner.
6 7 8 9 |
# File 'lib/redis_pipeliner/pipeliner.rb', line 6 def initialize(redis) @redis = redis @commands = [] end |
Instance Method Details
#commands ⇒ Object
Returns the enqueue REDIS commands
39 40 41 |
# File 'lib/redis_pipeliner/pipeliner.rb', line 39 def commands @commands.map {|h| h[:future] } end |
#enqueue(future, &proc) ⇒ Object Also known as: <<
Adds a command (a Future, actually) with an option block to call when the Future has been realized.
12 13 14 |
# File 'lib/redis_pipeliner/pipeliner.rb', line 12 def enqueue(future, &proc) @commands << { future: future, callback: proc } end |
#values ⇒ Object Also known as: wait
Blocks until all Futures have been realized and returns the values. This should be called outside the Redis.pipelined call. Note that the value enqueue is the REDIS return value, not the value returned by any passed block. Nil values will be included in the return values (if that’s what REDIS gives us).
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/redis_pipeliner/pipeliner.rb', line 21 def values return @values unless @values.nil? @values = [] @commands.each do |cmd| while cmd[:future].value.is_a?(Redis::FutureNotReady) sleep(1.0 / 100.0) end v = cmd[:future].value cmd[:callback].call(v) unless cmd[:callback].nil? @values << v end @values end |