Module: EventMachine::Protocols::Memcache::Sender
- Included in:
- Connectable
- Defined in:
- lib/evented_memcache_client/sender.rb
Overview
Interface for sending memcache protocol messages like STORE, END, etc. See code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt for more details.
NB: since several methods are defined dynamically here using define_method, they don’t show up in the RDoc-generated documentation. Sorry.
Instance Method Summary collapse
-
#book_it(msg_type) ⇒ Object
Keeps stats on message sends.
-
#cas(*args) ⇒ Object
Send a cas request to the memcached server.
-
#delete(*args) ⇒ Object
Send a delete request to the memcached server.
-
#get(args) ⇒ Object
(also: #gets)
Send a get request to the memcached server.
-
#send_to_peer(data) ⇒ Object
Send a memcache protocol message contained in the :data argument to the remote endpoint.
-
#stats(*args) ⇒ Object
Send a stats request to the memcached server.
-
#value(args) ⇒ Object
Send a value message to the remote endpoint (client); this is a server-sent message.
Instance Method Details
#book_it(msg_type) ⇒ Object
Keeps stats on message sends.
39 40 41 42 |
# File 'lib/evented_memcache_client/sender.rb', line 39 def book_it(msg_type) @snd_stats[msg_type] ||= 0 @snd_stats[msg_type] += 1 end |
#cas(*args) ⇒ Object
Send a cas request to the memcached server.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/evented_memcache_client/sender.rb', line 64 def cas(*args) book_it(:cas) key = args[:key] flags = args[:flags] || 0 exptime = args[:exptime] || 0 cas_uniq = args[:cas_uniq] noreply = args[:noreply] || false data = args[:data] raise ArgumentError.new(':data cannot be nil') unless data raise ArgumentError.new(':key cannot be nil') unless key raise ArgumentError.new(':cas_uniq cannot be nil') unless cas_uniq cmd_str = "cas #{key} #{flags} #{exptime} #{data.length} #{cas_uniq}" cmd_str << " noreply" if noreply send_to_peer "#{cmd_str}\r\n#{data}\r\n" end |
#delete(*args) ⇒ Object
Send a delete request to the memcached server. args
is a Hash with a required :key member, and optional :time and :noreply members.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/evented_memcache_client/sender.rb', line 98 def delete(*args) book_it(:delete) key = args[:key] time = args[:time] || 0 noreply = args[:noreply] || false raise ArgumentError.new(':key cannot be nil') unless key cmd_str = "delete #{key}" cmd_str << " #{time}" if time > 0 cmd_str << " noreply" if noreply send_to_peer "#{cmd_str}\r\n" end |
#get(args) ⇒ Object Also known as: gets
Send a get request to the memcached server. args
must have a :keys key if it is a hash, otherwise it’s assumed that it is a single key. Eg., get(‘queue’) or get(:keys=>[‘queue_a’, ‘queue_b’]) or get(:key=>‘queue’).
84 85 86 87 88 89 90 91 92 |
# File 'lib/evented_memcache_client/sender.rb', line 84 def get(args) if args.respond_to?(:keys) keys = args[:keys] || [args[:key]] else keys = [args] end raise ArgumentError.new('No keys specified.') if keys.empty? send_to_peer "get #{keys.join(' ')}\r\n" end |
#send_to_peer(data) ⇒ Object
Send a memcache protocol message contained in the :data argument to the remote endpoint.
176 177 178 179 180 |
# File 'lib/evented_memcache_client/sender.rb', line 176 def send_to_peer(data) @msgs_out ||= 0 @msgs_out += 1 send_data(data) end |
#stats(*args) ⇒ Object
Send a stats request to the memcached server. args
is a Array that specifies the specific stats command as defined in the memcache protocol.
130 131 132 133 134 |
# File 'lib/evented_memcache_client/sender.rb', line 130 def stats(*args) book_it(:stats) cmd_str = "stats #{args.join(' ')}" send_to_peer "#{cmd_str}\r\n" end |
#value(args) ⇒ Object
Send a value message to the remote endpoint (client); this is a server-sent message. args
is a Hash with a required :key member, and optional :flash and :cas_uniq members. Hash must also include a :data member (the payload to be sent).
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/evented_memcache_client/sender.rb', line 161 def value(args) book_it(:value) key = args[:key] flags = args[:flags] || 0 cas_uniq = args[:cas_uniq] data = args[:data] raise ArgumentError.new(':data cannot be nil') unless data raise ArgumentError.new(':key cannot be nil') unless key cmd_str = "VALUE #{key} #{flags} #{data.length}" cmd_str << " #{cas_uniq}" if cas_uniq send_to_peer "#{cmd_str}\r\n#{data}\r\n" end |