Implements the Memcache protocol (code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt). Requires memcached >= 1.2.4 w/ noreply support
EM.run{ cache = EM::P::Memcache.connect 'localhost', 11211 cache.set :a, 'hello' cache.set :b, 'hi' cache.set :c, 'how are you?' cache.set :d, '' cache.get(:a){ |v| p v } cache.get_hash(:a, :b, :c, :d){ |v| p v } cache.get(:a,:b,:c,:d){ |a,b,c,d| p [a,b,c,d] } cache.get(:a,:z,:b,:y,:d){ |a,z,b,y,d| p [a,z,b,y,d] } cache.get(:missing){ |m| p [:missing=, m] } cache.set(:missing, 'abc'){ p :stored } cache.get(:missing){ |m| p [:missing=, m] } cache.del(:missing){ p :deleted } cache.get(:missing){ |m| p [:missing=, m] } }
Delete the value associated with a key
cache.del :a cache.del(:b){ puts "deleted the value!" }
# File lib/em/protocols/memcache.rb, line 100 100: def delete key, expires = 0, &cb 101: callback{ 102: send_data "delete #{key} #{expires}#{cb ? '' : ' noreply'}\r\n" 103: @del_cbs << cb if cb 104: } 105: end
Get the value associated with one or multiple keys
cache.get(:a){ |v| p v } cache.get(:a,:b,:c,:d){ |a,b,c,d| p [a,b,c,d] }
# File lib/em/protocols/memcache.rb, line 56 56: def get *keys 57: raise ArgumentError unless block_given? 58: 59: callback{ 60: keys = keys.map{|k| k.to_s.gsub(/\s/,'_') } 61: send_data "get #{keys.join(' ')}\r\n" 62: @get_cbs << [keys, proc{ |values| 63: yield *keys.map{ |k| values[k] } 64: }] 65: } 66: end
Gets multiple values as a hash
cache.get_hash(:a, :b, :c, :d){ |h| puts h[:a] }
# File lib/em/protocols/memcache.rb, line 87 87: def get_hash *keys 88: raise ArgumentError unless block_given? 89: 90: get *keys do |*values| 91: yield keys.inject({}){ |hash, k| hash.update k => values[keys.index(k)] } 92: end 93: end
Set the value for a given key
cache.set :a, 'hello' cache.set(:missing, 'abc'){ puts "stored the value!" }
# File lib/em/protocols/memcache.rb, line 73 73: def set key, val, exptime = 0, &cb 74: callback{ 75: val = val.to_s 76: send_cmd :set, key, 0, exptime, val.respond_to?(:bytesize) ? val.bytesize : val.size, !block_given? 77: send_data val 78: send_data Cdelimiter 79: @set_cbs << cb if cb 80: } 81: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.