Class: Memcache::Server
Defined Under Namespace
Classes: ClientError, MemcacheError, ServerError
Constant Summary
collapse
- CONNECT_TIMEOUT =
1.0
- READ_RETRY_DELAY =
5.0
- DEFAULT_PORT =
11211
Instance Attribute Summary collapse
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
-
#close(error = nil) ⇒ Object
-
#decr(key, amount = 1) ⇒ Object
-
#delete(key) ⇒ Object
-
#flush_all(delay = nil) ⇒ Object
(also: #clear)
-
#get(keys, cas = false) ⇒ Object
-
#gets(keys) ⇒ Object
-
#incr(key, amount = 1) ⇒ Object
-
#initialize(opts) ⇒ Server
constructor
A new instance of Server.
-
#inspect ⇒ Object
-
#name ⇒ Object
-
#prepend(key, value) ⇒ Object
-
#readonly? ⇒ Boolean
-
#replace(key, value, expiry = 0, flags = 0) ⇒ Object
-
#set(key, value, expiry = 0, flags = 0) ⇒ Object
-
#stats ⇒ Object
-
#strict_reads? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ Server
Returns a new instance of Server.
18
19
20
21
22
23
24
|
# File 'lib/memcache/server.rb', line 18
def initialize(opts)
@host = opts[:host]
@port = opts[:port] || DEFAULT_PORT
@readonly = opts[:readonly]
@strict_reads = opts[:strict_reads]
@status = 'NOT CONNECTED'
end
|
Instance Attribute Details
Returns the value of attribute host.
11
12
13
|
# File 'lib/memcache/server.rb', line 11
def host
@host
end
|
Returns the value of attribute port.
11
12
13
|
# File 'lib/memcache/server.rb', line 11
def port
@port
end
|
Returns the value of attribute retry_at.
11
12
13
|
# File 'lib/memcache/server.rb', line 11
def retry_at
@retry_at
end
|
Returns the value of attribute status.
11
12
13
|
# File 'lib/memcache/server.rb', line 11
def status
@status
end
|
#strict_reads=(value) ⇒ Object
Sets the attribute strict_reads
12
13
14
|
# File 'lib/memcache/server.rb', line 12
def strict_reads=(value)
@strict_reads = value
end
|
Instance Method Details
#add(key, value, expiry = 0, flags = 0) ⇒ Object
141
142
143
144
145
|
# File 'lib/memcache/server.rb', line 141
def add(key, value, expiry = 0, flags = 0)
check_writable!
response = write_command("add #{key} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value)
response == "STORED\r\n" ? value : nil
end
|
#alive? ⇒ Boolean
34
35
36
|
# File 'lib/memcache/server.rb', line 34
def alive?
@retry_at.nil? or @retry_at < Time.now
end
|
#append(key, value) ⇒ Object
153
154
155
156
157
|
# File 'lib/memcache/server.rb', line 153
def append(key, value)
check_writable!
response = write_command("append #{key} 0 0 #{value.to_s.size}", value)
response == "STORED\r\n"
end
|
#cas(key, value, cas, expiry = 0, flags = 0) ⇒ Object
135
136
137
138
139
|
# File 'lib/memcache/server.rb', line 135
def cas(key, value, cas, expiry = 0, flags = 0)
check_writable!
response = write_command("cas #{key} #{flags.to_i} #{expiry.to_i} #{value.to_s.size} #{cas.to_i}", value)
response == "STORED\r\n" ? value : nil
end
|
#close(error = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/memcache/server.rb', line 46
def close(error = nil)
@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.message, @retry_at]
else
@retry_at = nil
@status = "NOT CONNECTED"
end
end
|
#decr(key, amount = 1) ⇒ Object
116
117
118
119
120
121
|
# File 'lib/memcache/server.rb', line 116
def decr(key, amount = 1)
check_writable!
raise MemcacheError, "decr requires unsigned value" if amount < 0
response = write_command("decr #{key} #{amount}")
response == "NOT_FOUND\r\n" ? nil : response.to_i
end
|
#delete(key) ⇒ Object
123
124
125
126
|
# File 'lib/memcache/server.rb', line 123
def delete(key)
check_writable!
write_command("delete #{key}") == "DELETED\r\n"
end
|
#flush_all(delay = nil) ⇒ Object
Also known as:
clear
76
77
78
79
|
# File 'lib/memcache/server.rb', line 76
def flush_all(delay = nil)
check_writable!
write_command("flush_all #{delay}")
end
|
#get(keys, cas = false) ⇒ Object
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 87
def get(keys, cas = false)
return get([keys], cas)[keys.to_s] unless keys.kind_of?(Array)
return {} if keys.empty?
method = cas ? 'gets' : 'get'
results = {}
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)
value.memcache_flags = flags.to_i
value.memcache_cas = cas
results[key] = value
end
results
end
|
#gets(keys) ⇒ Object
83
84
85
|
# File 'lib/memcache/server.rb', line 83
def gets(keys)
get(keys, true)
end
|
#incr(key, amount = 1) ⇒ Object
109
110
111
112
113
114
|
# File 'lib/memcache/server.rb', line 109
def incr(key, amount = 1)
check_writable!
raise MemcacheError, "incr requires unsigned value" if amount < 0
response = write_command("incr #{key} #{amount}")
response == "NOT_FOUND\r\n" ? nil : response.to_i
end
|
26
27
28
|
# File 'lib/memcache/server.rb', line 26
def inspect
"<Memcache::Server: %s:%d (%s)>" % [@host, @port, @status]
end
|
30
31
32
|
# File 'lib/memcache/server.rb', line 30
def name
"#{host}:#{port}"
end
|
#prepend(key, value) ⇒ Object
159
160
161
162
163
|
# File 'lib/memcache/server.rb', line 159
def prepend(key, value)
check_writable!
response = write_command("prepend #{key} 0 0 #{value.to_s.size}", value)
response == "STORED\r\n"
end
|
#readonly? ⇒ Boolean
38
39
40
|
# File 'lib/memcache/server.rb', line 38
def readonly?
@readonly
end
|
#replace(key, value, expiry = 0, flags = 0) ⇒ Object
147
148
149
150
151
|
# File 'lib/memcache/server.rb', line 147
def replace(key, value, expiry = 0, flags = 0)
check_writable!
response = write_command("replace #{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
128
129
130
131
132
133
|
# File 'lib/memcache/server.rb', line 128
def set(key, value, expiry = 0, flags = 0)
return delete(key) if value.nil?
check_writable!
write_command("set #{key} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value)
value
end
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/memcache/server.rb', line 60
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
42
43
44
|
# File 'lib/memcache/server.rb', line 42
def strict_reads?
@strict_reads
end
|