Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first take on remcached wrapper (working get)

  • Loading branch information...
commit 113a3434f24e3db8809a66aebb3fe06636b7d20a 1 parent 036e1d5
@igrigorik authored
View
2  lib/em-synchrony.rb
@@ -8,6 +8,6 @@
require "em-synchrony/em-http"
require "em-synchrony/em-mysql"
# require "em-synchrony/em-jack"
-# require "em-synchrony/em-memcached"
+require "em-synchrony/em-remcached"
require "em-synchrony/connection_pool"
View
0  lib/em-synchrony/em-memcached.rb
No changes.
View
45 lib/em-synchrony/em-remcached-fail.rb
@@ -1,45 +0,0 @@
-# There are a number of em-memcache libraries:
-# - http://github.com/astro/remcached/ -- Most feature complete, up-to-date: binary protocol, etc.
-# * http://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol
-# * http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
-#
-# - http://github.com/cliffmoon/eventedcache/ -- Older, but good specs + ragel parser for the protocol.
-#
-
-require 'remcached'
-
-module Memcached
- class << self
-
- def connect(servers)
- Memcached.servers = servers
-
- f = Fiber.current
- @t = EM::PeriodicTimer.new(0.01) do
- if Memcached.usable?
- @t.cancel
- f.resume(self)
- end
- end
-
- Fiber.yield
- end
-
- alias :aoperation :operation
- def operation(request_klass, contents, &blk)
- f = Fiber.current
- cb = Proc.new {|r| f.resume(r)}
-
- client = client_for_key(contents[:key])
- p [client, contents, blk]
- if client
- client.send_request request_klass.new(contents), &cb
- else
- return {:status => Errors::DISCONNECTED}
- end
-
- Fiber.yield
- end
-
- end
-end
View
49 lib/em-synchrony/em-remcached.rb
@@ -0,0 +1,49 @@
+require "remcached"
+
+module Memcached
+ class << self
+
+ def connect(servers)
+ Memcached.servers = servers
+
+ f = Fiber.current
+ @t = EM::PeriodicTimer.new(0.01) do
+ if Memcached.usable?
+ @t.cancel
+ f.resume(self)
+ end
+ end
+
+ Fiber.yield
+ end
+
+ def aget(contents, &callback)
+ df = EventMachine::DefaultDeferrable.new
+ df.callback &callback
+
+ cb = Proc.new { |res| df.succeed(res) }
+ operation Request::Get, contents, &cb
+
+ df
+ end
+
+ def get(contents, &callback)
+ fiber = Fiber.current
+
+ df = aget(contents, &Proc.new { |res| fiber.resume(res) })
+ df.callback &callback
+
+ Fiber.yield
+ end
+
+
+ # def set(contents, &callback)
+ # operation Request::Set, contents, &callback
+ # end
+ # def delete(contents, &callback)
+ # operation Request::Delete, contents, &callback
+ # end
+
+
+ end
+end
View
44 spec/memcached_spec.rb
@@ -1,44 +0,0 @@
-require 'spec/helper'
-
-describe Memcached do
-
- it "should fire sequential memcached requests" do
- EventMachine.run do
- Fiber.new {
-
-
- Memcached.connect(%w(127.0.0.1:11211))
- p Memcached.usable?
-
- # p Memcached.set({:key => 'hai', :value => 'win'})
- # p Memcached.set(:key => 'Hello', :value => 'Planet')
- p Memcached.get(:key => 'hai')
-
- EventMachine.stop
- }.resume
- end
- end
-
- # it "should fire multiple requests in parallel" do
- # pending
- #
- # EventMachine.run do
- #
- # Fiber.new {
- # jack = EMJack::Connection.new
- #
- # multi = EventMachine::Multi.new
- # multi.add jack.ause('mytube-1')
- # multi.add jack.ause('mytube-2')
- # res = multi.perform
- #
- # res.responses.size.should == 2
- # p [:multi, res.responses]
- #
- # EventMachine.stop
- # }.resume
- #
- # end
- # end
-
-end
View
1  spec/mysql_spec.rb
@@ -75,7 +75,6 @@
start = now
- # TODO: need pooling logic
multi = EventMachine::Synchrony::Multi.new
multi.add :a, db.aquery(QUERY)
multi.add :b, db.aquery(QUERY)
View
36 spec/remcached_spec.rb
@@ -0,0 +1,36 @@
+require "spec/helper/all"
+require "remcached"
+
+describe Memcached do
+
+ it "should yield until connection is ready" do
+ EventMachine.run do
+ Fiber.new {
+ Memcached.connect %w(localhost)
+ Memcached.usable?.should be_true
+ EventMachine.stop
+ }.resume
+ end
+ end
+
+ it "should fire sequential memcached requests" do
+ EventMachine.run do
+ Fiber.new {
+
+ Memcached.connect %w(localhost)
+
+ Memcached.get(:key => 'hai') do |res|
+ res[:value].should match('Not found')
+ end
+
+ # p Memcached.set({:key => 'hai', :value => 'win'})
+ # p Memcached.set(:key => 'Hello', :value => 'Planet')
+
+
+
+ EventMachine.stop
+ }.resume
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.