class RubyMemcached::Server
Attributes
host[R]
port[R]
storage[RW]
Public Class Methods
new(host, port)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 13 def initialize(host, port) @host = host @port = port @connection = TCPServer.new(host, port) @memc = Memcached.new() end
Public Instance Methods
add(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 119 def add(client, command) key = command['key'] flags = command['flags'] exptime = command['exptime'] bytes = command['bytes'].to_i() data = self.get_data(client, bytes) noreply = !command['noreply'].nil? response = @memc.add(key, flags, exptime, bytes, data) client.puts(response) unless noreply end
append(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 143 def append(client, command) key = command['key'] bytes = command['bytes'].to_i() data = self.get_data(client, bytes) noreply = !command['noreply'].nil? response = @memc.append(key, bytes, data) client.puts(response) unless noreply end
cas(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 164 def cas(client, command) key = command['key'] flags = command['flags'] exptime = command['exptime'] bytes = command['bytes'].to_i() data = self.get_data(client, bytes) cas_id = command['cas_id'].to_i() noreply = !command['noreply'].nil? response = @memc.cas(key, flags, exptime, bytes, cas_id, data) client.puts(response) unless noreply end
delete(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 177 def delete(client, command) key = command['key'] noreply = !command['noreply'].nil? response = @memc.delete(key) client.puts(response) unless noreply end
get(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 87 def get(client, command) keys = command['keys'].split(' ') items = @memc.get_multi(keys) for item in items client.puts(Responses.get % [item.key, item.flags, item.bytes, item.data]) unless item.nil?() end client.puts(Responses.end) end
get_data(client, bytes)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 83 def get_data(client, bytes) return client.read(bytes + 1).chomp() end
gets(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 97 def gets(client, command) keys = command['keys'].split(' ') items = @memc.get_multi(keys) for item in items client.puts(Responses.gets % [item.key, item.flags, item.bytes, item.cas_id, item.data]) unless item.nil?() end client.puts(Responses.end) end
parse(command, client)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 45 def parse(command, client) case command when CommandsRegex.get self.get(client, $~) when CommandsRegex.gets self.gets(client, $~) when CommandsRegex.set self.set(client, $~) when CommandsRegex.add self.add(client, $~) when CommandsRegex.replace self.replace(client, $~) when CommandsRegex.append self.append(client, $~) when CommandsRegex.prepend self.prepend(client, $~) when CommandsRegex.cas self.cas(client, $~) when CommandsRegex.delete self.delete(client, $~) when CommandsRegex.end return false; else client.puts(Errors.client_error % [": Invalid command"]) end return true end
prepend(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 153 def prepend(client, command) key = command['key'] bytes = command['bytes'].to_i() data = self.get_data(client, bytes) noreply = !command['noreply'].nil? response = @memc.prepend(key, bytes, data) client.puts(response) unless noreply end
replace(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 131 def replace(client, command) key = command['key'] flags = command['flags'] exptime = command['exptime'] bytes = command['bytes'].to_i() data = self.get_data(client, bytes) noreply = !command['noreply'].nil? response = @memc.replace(key, flags, exptime, bytes, data) client.puts(response) unless noreply end
set(client, command)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 107 def set(client, command) key = command['key'] flags = command['flags'] exptime = command['exptime'] bytes = command['bytes'].to_i() data = self.get_data(client, bytes) noreply = !command['noreply'].nil? response = @memc.set(key, flags, exptime, bytes, data) client.puts(response) unless noreply end
start()
click to toggle source
# File lib/ruby-memcached/Server.rb, line 20 def start loop do Thread.start(@connection.accept()) do | client | puts('Opening connection to %s' % client.to_s) continue_condition = true while command = client.gets() printf('%s: %s' % [client.to_s(), command]) begin continue_condition = parse(command, client) unless command.nil? rescue => e client.puts(Errors.server_error % e.message) puts(Errors.server_error % e.message) end break unless continue_condition end client.close() puts('Closing connection to %s' % [client.to_s()]) end end end
start_gc(interval, loops = nil)
click to toggle source
# File lib/ruby-memcached/Server.rb, line 185 def start_gc(interval, loops = nil) while loops.nil?() || loops > 0 sleep(interval) puts('Garbage collector deleted: %d' % @memc.check_exptimes()) loops -= 1 unless loops.nil?() end end