Class: Memcache::Server
Direct Known Subclasses
Constant Summary collapse
- CONNECT_TIMEOUT =
1.0
- READ_RETRY_DELAY =
5.0
- DEFAULT_PORT =
11211
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#retry_at ⇒ Object
readonly
Returns the value of attribute retry_at.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Attributes inherited from Base
Instance Method Summary collapse
- #add(key, value, expiry = 0, flags = 0) ⇒ Object
- #alive? ⇒ Boolean
- #append(key, value) ⇒ Object
- #cas(key, value, cas, expiry = 0, flags = 0) ⇒ Object
- #clone ⇒ Object
- #close(error = nil) ⇒ Object
- #count ⇒ Object
- #decr(key, amount = 1) ⇒ Object
- #delete(key) ⇒ Object
- #flush_all(delay = nil) ⇒ Object
- #get(keys, cas = nil) ⇒ Object
- #incr(key, amount = 1) ⇒ Object
-
#initialize(opts) ⇒ Server
constructor
A new instance of Server.
- #inspect ⇒ Object
- #name ⇒ Object
- #prepend(key, value) ⇒ Object
- #replace(key, value, expiry = 0, flags = 0) ⇒ Object
- #set(key, value, expiry = 0, flags = 0) ⇒ Object
- #stats ⇒ Object
- #strict_reads? ⇒ Boolean
Methods inherited from Base
Constructor Details
#initialize(opts) ⇒ Server
Returns a new instance of Server.
13 14 15 16 17 18 |
# File 'lib/memcache/server.rb', line 13 def initialize(opts) @host = opts[:host] @port = opts[:port] || DEFAULT_PORT @strict_reads = opts[:strict_reads] @status = 'NOT CONNECTED' end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
11 12 13 |
# File 'lib/memcache/server.rb', line 11 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
11 12 13 |
# File 'lib/memcache/server.rb', line 11 def port @port end |
#retry_at ⇒ Object (readonly)
Returns the value of attribute retry_at.
11 12 13 |
# File 'lib/memcache/server.rb', line 11 def retry_at @retry_at end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
11 12 13 |
# File 'lib/memcache/server.rb', line 11 def status @status end |
Instance Method Details
#add(key, value, expiry = 0, flags = 0) ⇒ Object
136 137 138 139 |
# File 'lib/memcache/server.rb', line 136 def add(key, value, expiry = 0, flags = 0) response = write_command("add #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value) response == "STORED\r\n" ? value : nil end |
#alive? ⇒ Boolean
32 33 34 |
# File 'lib/memcache/server.rb', line 32 def alive? @retry_at.nil? or @retry_at < Time.now end |
#append(key, value) ⇒ Object
146 147 148 149 |
# File 'lib/memcache/server.rb', line 146 def append(key, value) response = write_command("append #{cache_key(key)} 0 0 #{value.to_s.size}", value) response == "STORED\r\n" end |
#cas(key, value, cas, expiry = 0, flags = 0) ⇒ Object
131 132 133 134 |
# File 'lib/memcache/server.rb', line 131 def cas(key, value, cas, expiry = 0, flags = 0) response = write_command("cas #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size} #{cas.to_i}", value) response == "STORED\r\n" ? value : nil end |
#clone ⇒ Object
20 21 22 |
# File 'lib/memcache/server.rb', line 20 def clone self.class.new(:host => host, :port => port, :strict_reads => strict_reads?) end |
#close(error = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/memcache/server.rb', line 40 def close(error = nil) # Close the socket. If there is an error, mark the server dead. @socket.close if @socket and not @socket.closed? @socket = nil if error @retry_at = Time.now + READ_RETRY_DELAY @status = "DEAD: %s: %s, will retry at %s" % [error.class, error., @retry_at] else @retry_at = nil @status = "NOT CONNECTED" end end |
#count ⇒ Object
70 71 72 |
# File 'lib/memcache/server.rb', line 70 def count stats['curr_items'] end |
#decr(key, amount = 1) ⇒ Object
115 116 117 118 119 |
# File 'lib/memcache/server.rb', line 115 def decr(key, amount = 1) raise Error, "decr requires unsigned value" if amount < 0 response = write_command("decr #{cache_key(key)} #{amount}") response == "NOT_FOUND\r\n" ? nil : response.slice(0..-3).to_i end |
#delete(key) ⇒ Object
121 122 123 |
# File 'lib/memcache/server.rb', line 121 def delete(key) write_command("delete #{cache_key(key)}") == "DELETED\r\n" ? true : nil end |
#flush_all(delay = nil) ⇒ Object
74 75 76 |
# File 'lib/memcache/server.rb', line 74 def flush_all(delay = nil) write_command("flush_all #{delay}") end |
#get(keys, cas = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/memcache/server.rb', line 78 def get(keys, cas = nil) return get([keys], cas)[keys.to_s] unless keys.kind_of?(Array) return {} if keys.empty? method = cas ? 'gets' : 'get' results = {} keys = keys.collect {|key| cache_key(key)} read_command("#{method} #{keys.join(' ')}") do |response| if cas key, flags, length, cas = match_response!(response, /^VALUE ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)/) else key, flags, length = match_response!(response, /^VALUE ([^\s]+) ([^\s]+) ([^\s]+)/) end value = socket.read(length.to_i) match_response!(socket.read(2), "\r\n") result = { :value => value, :flags => flags.to_i, } result[:cas] = cas if cas key = input_key(key) results[key] = result end results end |
#incr(key, amount = 1) ⇒ Object
109 110 111 112 113 |
# File 'lib/memcache/server.rb', line 109 def incr(key, amount = 1) raise Error, "incr requires unsigned value" if amount < 0 response = write_command("incr #{cache_key(key)} #{amount}") response == "NOT_FOUND\r\n" ? nil : response.slice(0..-3).to_i end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/memcache/server.rb', line 24 def inspect "<#{self.class.name}: %s:%d (%s)>" % [@host, @port, @status] end |
#name ⇒ Object
28 29 30 |
# File 'lib/memcache/server.rb', line 28 def name "#{host}:#{port}" end |
#prepend(key, value) ⇒ Object
151 152 153 154 |
# File 'lib/memcache/server.rb', line 151 def prepend(key, value) response = write_command("prepend #{cache_key(key)} 0 0 #{value.to_s.size}", value) response == "STORED\r\n" end |
#replace(key, value, expiry = 0, flags = 0) ⇒ Object
141 142 143 144 |
# File 'lib/memcache/server.rb', line 141 def replace(key, value, expiry = 0, flags = 0) response = write_command("replace #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value) response == "STORED\r\n" ? value : nil end |
#set(key, value, expiry = 0, flags = 0) ⇒ Object
125 126 127 128 129 |
# File 'lib/memcache/server.rb', line 125 def set(key, value, expiry = 0, flags = 0) return delete(key) if value.nil? write_command("set #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value) value end |
#stats ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/memcache/server.rb', line 54 def stats stats = {} read_command('stats') do |response| key, value = match_response!(response, /^STAT ([\w]+) (-?[\w\.\:]+)/) if ['rusage_user', 'rusage_system'].include?(key) seconds, microseconds = value.split(/:/, 2) microseconds ||= 0 stats[key] = Float(seconds) + (Float(microseconds) / 1_000_000) else stats[key] = (value =~ /^-?\d+$/ ? value.to_i : value) end end stats end |
#strict_reads? ⇒ Boolean
36 37 38 |
# File 'lib/memcache/server.rb', line 36 def strict_reads? @strict_reads end |