Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
masuidrive committed Oct 21, 2008
0 parents commit b185ad9
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
55 changes: 55 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
= em-memcache-client
Async memcache driver for Ruby EventMachine.

Just now, experimental build.


== DESCRIPTION:

FIX (describe your package)


== FEATURES/PROBLEMS:

* FIX (list of features or problems)


== SYNOPSIS:

* FIX (code sample of usage)


== REQUIREMENTS:

* FIX (list of requirements)


== INSTALL:

* FIX (sudo gem install, anything else)


== LICENSE:

(The MIT License)

Copyright (c) 2008: {Yuichiro MASUI a.k.a. masuidrive}[http://masuidrive.jp]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
118 changes: 118 additions & 0 deletions lib/em/memcache-client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require 'eventmachine'
require 'strscan'

class EventMachine::MemcacheClient < EM::Connection
include EM::Protocols::LineText2

Task = Struct.new(:accepts, :options, :callback, :multiline)

private
def send_store_cmd(cmd, key, value, options, &block)
value = Marshal.dump(value) unless options[:raw]
send_data "#{cmd} #{key} #{options[:flags] || 0} #{options[:expire] || 0} #{value.size}#{block ? nil : ' noreply'}\r\n#{value}\r\n"
@queue << Task.new(['stored', 'not_stored', 'exists', 'not_found'], options, block) if block
end

public
def set(key, value, options={}, &block)
send_store_cmd('set', key, value, options, &block)
end

def get(key, options={}, &block)
send_data "get #{key}\r\n"
@queue << Task.new(['value'], options, block, true)
end

def delete(key, options={}, &block)
send_data "delete #{key}#{block ? nil : 'noreply'}\r\n"
@queue << Task.new(['deleted','not_found'], options, block) if block
end

def receive_line(line)
if @multiline || (@queue.size>0 && @queue.first.multiline && line=='END')
if line=='END'
cmd, task, params = @multiline || ['value', @queue.shift, nil]
send("#{cmd}_handler", task, params, @data)
@multiline = @data = nil, ""
else
@data << line
end

elsif @queue.size>0
task = @queue.shift
cmd, *params = line.split(' ')
cmd.downcase!
if task.accepts.include?(cmd)
if task.multiline
@multiline = [cmd, task, params]
else
send("#{cmd}_handler", task, params)
end
end
end
end

%w[ stored not_found deleted ].each do |type| class_eval %[
def #{type}_handler(task, params)
task.callback.call if task.callback
end
] end

def value_handler(task, params, data)
task.callback.call(task.options[:raw] ? data : (data=="" ? nil : Marshal.load(data)))
end

def post_init
@data = ""
@queue, @multiline = [], nil
end
end

class EventMachine::MemcacheClient < EM::Connection
def self.settings
@settings ||= { :host => 'localhost', :port => 11211, :connections => 4, :logging => false }
end

%w[ get set delete ].each do |type| class_eval %[
def self.#{type}(*args, &block)
connection.send("#{type}",*args, &block)
end
] end

def self.connection
@n ||= 0
connection = connection_pool[@n]
@n = 0 if (@n+=1) >= connection_pool.size
connection
end

def self.connection_pool
@connection_pool ||= (1..settings[:connections]).map{ EM::connect(settings[:host], settings[:port], self) }
end
end

if $0 == __FILE__
EventMachine::run {
EventMachine::MemcacheClient.connection_pool
EventMachine::add_timer 1, proc {
EventMachine::MemcacheClient.delete('foo') do
puts "deleted foo"
end
}
EventMachine::add_timer 2, proc {
EventMachine::MemcacheClient.get('foo') do |value|
print "nil => "
p value
end
}
EventMachine::add_timer 3, proc {
EventMachine::MemcacheClient.set('foo', [1,2]) do
EventMachine::MemcacheClient.get('foo') do |value|
print "[1,2] => "
p value
EventMachine::stop
end
end
}
}
end

0 comments on commit b185ad9

Please sign in to comment.