Skip to content
Browse files

fix wrapper for em-redis' mapped_mget

  • Loading branch information...
1 parent e67bf77 commit 79b2f5d271f3c1c9e1884cbbab853ed11aa2c6b4 @bts bts committed
Showing with 51 additions and 2 deletions.
  1. +24 −1 lib/em-synchrony/em-redis.rb
  2. +27 −1 spec/redis_spec.rb
View
25 lib/em-synchrony/em-redis.rb
@@ -42,6 +42,29 @@ def call_command(argv, &blk)
Fiber.yield
end
end
+
+ # adapted from em-redis' implementation to use
+ # the asynchronous version of mget
+ def amapped_mget(*keys)
+ self.amget(*keys) do |response|
+ result = {}
+ response.each do |value|
+ key = keys.shift
+ result.merge!(key => value) unless value.nil?
+ end
+ yield result if block_given?
+ end
+ end
+
+ def mapped_mget(*keys)
+ f = Fiber.current
+
+ self.amapped_mget(*keys) do |values|
+ f.resume(values)
+ end
+
+ Fiber.yield
+ end
end
end
-end
+end
View
28 spec/redis_spec.rb
@@ -23,6 +23,19 @@
end
end
+ it "should mapped_mget synchronously" do
+ EventMachine.synchrony do
+ redis = EM::Protocols::Redis.connect
+
+ redis.set('mmget1', 'value1')
+ redis.set('mmget3', 'value3')
+ redis.mapped_mget('mmget1', 'mmget2', 'mmget3').should ==
+ { 'mmget1' => 'value1', 'mmget3' => 'value3' }
+
+ EM.stop
+ end
+ end
+
it "should incr/decr key synchronously" do
EventMachine.synchrony do
redis = EM::Protocols::Redis.connect
@@ -63,4 +76,17 @@
end
end
end
-end
+
+ it "should execute async mapped_mget" do
+ EventMachine.synchrony do
+ redis = EM::Protocols::Redis.connect
+
+ redis.aset('some_key', 'some_value') do
+ redis.amapped_mget('some_key', 'some_other_key') do |values|
+ values.should == { 'some_key' => 'some_value' }
+ EM.stop
+ end
+ end
+ end
+ end
+end

0 comments on commit 79b2f5d

Please sign in to comment.
Something went wrong with that request. Please try again.