Permalink
Browse files

add em-memcache wrapper around EM::P::Memcache as an option for users…

… on memcached < 1.3
  • Loading branch information...
1 parent fd90d1b commit 276220b5655a17f79666c770083609502c0f04c6 @bts bts committed with bts Aug 13, 2010
Showing with 53 additions and 0 deletions.
  1. +19 −0 lib/em-synchrony/em-memcache.rb
  2. +1 −0 spec/helper/all.rb
  3. +33 −0 spec/memcache_spec.rb
@@ -0,0 +1,19 @@
+module EventMachine::Protocols::Memcache
+ %w[delete get set].each do |type|
+ module_eval %[
+ alias :a#{type} :#{type}
+ def #{type}(*params, &blk)
+ f = Fiber.current
+ self.a#{type}(*params) { |*cb_params| f.resume(*cb_params) }
+
+ Fiber.yield
+ end
+ ]
+ end
+
+ alias :aget_hash :get_hash
+ def get_hash(*keys)
+ index = 0
+ get(*keys).inject({}) { |h,v| h[keys[index]] = v; index += 1; h }
+ end
+end
View
@@ -6,6 +6,7 @@
require 'lib/em-synchrony/em-http'
require 'lib/em-synchrony/em-mysqlplus'
require 'lib/em-synchrony/em-remcached'
+require 'lib/em-synchrony/em-memcache'
require 'lib/em-synchrony/em-mongo'
require 'lib/em-synchrony/em-redis'
View
@@ -0,0 +1,33 @@
+require "spec/helper/all"
+
+describe EM::P::Memcache do
+
+ it "should fire sequential memcached requests" do
+ EventMachine.synchrony do
+ conn = EM::P::Memcache.connect
+ key = 'key'
+ value = 'value for key'
+ fake_key = 'nonexistent key' # without a corresponding value
+
+ conn.delete(key)
+ conn.set(key, value)
+ conn.get(key).should == value
+
+ conn.delete(key)
+ conn.get(key).should be_nil
+
+ conn.set(key, value)
+ conn.get(key).should == value
+
+ conn.del(key)
+ conn.get(key).should be_nil
+
+ conn.set(key, value)
+ conn.get(key, fake_key).should == [value, nil]
+ conn.get_hash(key, fake_key).should == { key => value, fake_key => nil }
+
+ EventMachine.stop
+ end
+ end
+
+end

0 comments on commit 276220b

Please sign in to comment.