Class: Memcache
- Inherits:
-
Object
show all
- Defined in:
- lib/memcache.rb,
lib/memcache/base.rb,
lib/memcache/server.rb,
lib/memcache/migration.rb,
lib/memcache/pg_server.rb,
lib/memcache/segmented.rb,
lib/memcache/null_server.rb,
lib/memcache/local_server.rb,
ext/native_server.c
Defined Under Namespace
Modules: Segmented
Classes: Base, ClientError, ConnectionError, Error, LocalServer, Migration, NativeServer, NullServer, PGServer, Pool, SegmentedNativeServer, SegmentedServer, Server, ServerError
Constant Summary
collapse
- DEFAULT_EXPIRY =
0
- LOCK_TIMEOUT =
5
- WRITE_LOCK_WAIT =
1
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
-
#add(key, value, opts = {}) ⇒ Object
-
#add_or_get(key, value, opts = {}) ⇒ Object
-
#append(key, value) ⇒ Object
-
#cas(key, value, opts) ⇒ Object
-
#clone ⇒ Object
-
#count(key) ⇒ Object
-
#decr(key, amount = 1) ⇒ Object
-
#delete(key) ⇒ Object
-
#flush_all(opts = {}) ⇒ Object
(also: #clear)
-
#get(keys, opts = {}) ⇒ Object
-
#get_or_add(key, *args) ⇒ Object
-
#get_or_set(key, *args) ⇒ Object
-
#get_some(keys, opts = {}) ⇒ Object
-
#in_namespace(namespace) ⇒ Object
-
#incr(key, amount = 1) ⇒ Object
-
#initialize(opts) ⇒ Memcache
constructor
A new instance of Memcache.
-
#inspect ⇒ Object
-
#lock(key, opts = {}) ⇒ Object
-
#lock_key(key) ⇒ Object
-
#locked?(key) ⇒ Boolean
-
#prepend(key, value) ⇒ Object
-
#read(key, opts = nil) ⇒ Object
-
#read_multi(*keys) ⇒ Object
-
#replace(key, value, opts = {}) ⇒ Object
-
#reset ⇒ Object
-
#set(key, value, opts = {}) ⇒ Object
-
#stats(field = nil) ⇒ Object
-
#unlock(key) ⇒ Object
-
#update(key, opts = {}) ⇒ Object
-
#with_lock(key, opts = {}) ⇒ Object
-
#write(key, value, opts = nil) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Memcache
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/memcache.rb', line 35
def initialize(opts)
@default_expiry = opts[:default_expiry] || DEFAULT_EXPIRY
@backup = opts[:backup] @hash_with_prefix = opts[:hash_with_prefix].nil? ? true : opts[:hash_with_prefix]
if opts[:native]
native_opts = opts.clone
native_opts[:servers] = (opts[:servers] || [ opts[:server] ]).collect do |server|
server.is_a?(Hash) ? "#{server[:host]}:#{server[:port]}:#{server[:weight]}" : server
end
native_opts[:hash] ||= :crc unless native_opts[:ketama] or native_opts[:ketama_wieghted]
native_opts[:hash_with_prefix] = @hash_with_prefix
server_class = opts[:segment_large_values] ? SegmentedNativeServer : NativeServer
@servers = [server_class.new(native_opts)]
else
raise "only CRC hashing is supported unless :native => true" if opts[:hash] and opts[:hash] != :crc
server_class = opts[:segment_large_values] ? SegmentedServer : Server
@servers = (opts[:servers] || [ opts[:server] ]).collect do |server|
case server
when Hash
server = server_class.new(opts.merge(server))
when String
host, port = server.split(':')
server = server_class.new(opts.merge(:host => host, :port => port))
when Class
server = server.new
when :local
server = Memcache::LocalServer.new
end
server
end
end
@server = @servers.first if @servers.size == 1 and @backup.nil?
self.namespace = opts[:namespace] if opts[:namespace]
end
|
Instance Attribute Details
Returns the value of attribute backup.
19
20
21
|
# File 'lib/memcache.rb', line 19
def backup
@backup
end
|
#default_expiry ⇒ Object
Returns the value of attribute default_expiry.
19
20
21
|
# File 'lib/memcache.rb', line 19
def default_expiry
@default_expiry
end
|
#namespace ⇒ Object
Returns the value of attribute namespace.
19
20
21
|
# File 'lib/memcache.rb', line 19
def namespace
@namespace
end
|
Returns the value of attribute servers.
19
20
21
|
# File 'lib/memcache.rb', line 19
def servers
@servers
end
|
Class Method Details
.init(yaml_file = nil) ⇒ Object
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
# File 'lib/memcache.rb', line 352
def self.init(yaml_file = nil)
yaml_file ||= File.join(Rails.root, 'config', 'memcached.yml')
if File.exists?(yaml_file)
yaml = YAML.load_file(yaml_file)
defaults = (yaml.delete('defaults') || {}).symbolize_keys
config = (yaml[Rails.env] || {}).symbolize_keys
if not config.empty? and not config[:disabled]
if config[:servers]
opts = defaults.merge(config.symbolize_keys)
Object.const_set('CACHE', Memcache.new(opts))
else
config.each do |connection, opts|
opts = defaults.merge(opts.symbolize_keys)
if not opts.empty? and not opts[:disabled]
Memcache.pool[connection] = Memcache.new(opts)
end
end
end
end
end
end
|
Instance Method Details
344
345
346
|
# File 'lib/memcache.rb', line 344
def [](key)
get(key)
end
|
#[]=(key, value) ⇒ Object
348
349
350
|
# File 'lib/memcache.rb', line 348
def []=(key, value)
set(key, value)
end
|
#add(key, value, opts = {}) ⇒ Object
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/memcache.rb', line 153
def add(key, value, opts = {})
opts = compatible_opts(opts)
key = key.to_s
backup.add(key, value, opts) if backup
expiry = opts[:expiry] || default_expiry
flags = opts[:flags] || 0
data = marshal(value, opts)
server(key).add(key, data, expiry, flags) && value
end
|
#add_or_get(key, value, opts = {}) ⇒ Object
252
253
254
255
|
# File 'lib/memcache.rb', line 252
def add_or_get(key, value, opts = {})
add(key, value, opts) || get(key)
end
|
#append(key, value) ⇒ Object
186
187
188
189
190
|
# File 'lib/memcache.rb', line 186
def append(key, value)
key = key.to_s
backup.append(key, value) if backup
server(key).append(key, value)
end
|
#cas(key, value, opts) ⇒ Object
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/memcache.rb', line 175
def cas(key, value, opts)
raise 'opts must be hash' unless opts.instance_of?(Hash)
key = key.to_s
backup.cas(key, value, opts) if backup
expiry = opts[:expiry] || default_expiry
flags = opts[:flags] || 0
data = marshal(value, opts)
server(key).cas(key, data, opts[:cas], expiry, flags) && value
end
|
74
75
76
77
78
79
80
|
# File 'lib/memcache.rb', line 74
def clone
self.class.new(
:default_expiry => default_expiry,
:namespace => namespace,
:servers => servers.collect {|s| s.clone}
)
end
|
#count(key) ⇒ Object
198
199
200
201
|
# File 'lib/memcache.rb', line 198
def count(key)
value = get(key, :raw => true)
value.to_i if value
end
|
#decr(key, amount = 1) ⇒ Object
211
212
213
214
215
216
217
|
# File 'lib/memcache.rb', line 211
def decr(key, amount = 1)
return incr(key, -amount) if amount < 0
key = key.to_s
backup.decr(key, amount) if backup
server(key).decr(key, amount)
end
|
#delete(key) ⇒ Object
308
309
310
311
312
|
# File 'lib/memcache.rb', line 308
def delete(key)
key = key.to_s
backup.delete(key) if backup
server(key).delete(key)
end
|
#flush_all(opts = {}) ⇒ Object
Also known as:
clear
314
315
316
317
318
319
320
321
322
|
# File 'lib/memcache.rb', line 314
def flush_all(opts = {})
delay = opts[:delay].to_i
interval = opts[:interval].to_i
servers.each do |server|
server.flush_all(delay)
delay += interval
end
end
|
#get(keys, opts = {}) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/memcache.rb', line 107
def get(keys, opts = {})
raise 'opts must be hash' unless opts.instance_of?(Hash)
if keys.instance_of?(Array)
keys = keys.collect {|key| key.to_s}
multi_get(keys, opts)
else
key = keys.to_s
if opts[:expiry]
value = server(key).gets(key)
cas(key, value, :raw => true, :cas => value.memcache_cas, :expiry => opts[:expiry]) if value
else
value = server(key).get(key, opts[:cas])
end
return backup.get(key, opts) if backup and value.nil?
opts[:raw] ? value : unmarshal(value, key)
end
end
|
#get_or_add(key, *args) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/memcache.rb', line 229
def get_or_add(key, *args)
key = key.to_s
if block_given?
opts = args[0] || {}
get(key) || add(key, yield, opts) || get(key)
else
opts = args[1] || {}
get(key) || add(key, args[0], opts) || get(key)
end
end
|
#get_or_set(key, *args) ⇒ Object
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/memcache.rb', line 241
def get_or_set(key, *args)
key = key.to_s
if block_given?
opts = args[0] || {}
get(key) || set(key, yield, opts)
else
opts = args[1] || {}
get(key) || set(key, args[0], opts)
end
end
|
#get_some(keys, opts = {}) ⇒ Object
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/memcache.rb', line 257
def get_some(keys, opts = {})
keys = keys.collect {|key| key.to_s}
records = opts[:disable] ? {} : self.multi_get(keys, opts)
if opts[:validation]
records.delete_if do |key, value|
not opts[:validation].call(key, value)
end
end
keys_to_fetch = keys - records.keys
if keys_to_fetch.any?
yield(keys_to_fetch).each do |key, value|
begin
set(key, value, {}) unless opts[:disable] or opts[:disable_write]
rescue Memcache::Error => e
raise if opts[:strict_write]
$stderr.puts "Memcache error in get_some: #{e.class} #{e.to_s} on key '#{key}' while storing value: #{value}"
end
records[key] = value
end
end
records
end
|
#in_namespace(namespace) ⇒ Object
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/memcache.rb', line 96
def in_namespace(namespace)
begin
old_namespace = self.namespace
self.namespace = old_namespace ? "#{old_namespace}:#{namespace}" : namespace
yield
ensure
self.namespace = old_namespace
end
end
|
#incr(key, amount = 1) ⇒ Object
203
204
205
206
207
208
209
|
# File 'lib/memcache.rb', line 203
def incr(key, amount = 1)
return decr(key, -amount) if amount < 0
key = key.to_s
backup.incr(key, amount) if backup
server(key).incr(key, amount)
end
|
82
83
84
|
# File 'lib/memcache.rb', line 82
def inspect
"<Memcache: %d servers, ns: %p>" % [@servers.length, namespace]
end
|
#lock(key, opts = {}) ⇒ Object
281
282
283
284
285
|
# File 'lib/memcache.rb', line 281
def lock(key, opts = {})
expiry = opts[:expiry] || LOCK_TIMEOUT
add(lock_key(key), Socket.gethostname, :expiry => expiry, :raw => true)
end
|
#lock_key(key) ⇒ Object
300
301
302
|
# File 'lib/memcache.rb', line 300
def lock_key(key)
"lock:#{key}"
end
|
#locked?(key) ⇒ Boolean
304
305
306
|
# File 'lib/memcache.rb', line 304
def locked?(key)
get(lock_key(key), :raw => true)
end
|
#prepend(key, value) ⇒ Object
192
193
194
195
196
|
# File 'lib/memcache.rb', line 192
def prepend(key, value)
key = key.to_s
backup.prepend(key, value) if backup
server(key).prepend(key, value)
end
|
#read(key, opts = nil) ⇒ Object
127
128
129
130
|
# File 'lib/memcache.rb', line 127
def read(key, opts = nil)
opts ||= {}
get(key, opts.merge(:raw => true))
end
|
#read_multi(*keys) ⇒ Object
132
133
134
|
# File 'lib/memcache.rb', line 132
def read_multi(*keys)
get(keys)
end
|
#replace(key, value, opts = {}) ⇒ Object
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/memcache.rb', line 164
def replace(key, value, opts = {})
opts = compatible_opts(opts)
key = key.to_s
backup.replace(key, value, opts) if backup
expiry = opts[:expiry] || default_expiry
flags = opts[:flags] || 0
data = marshal(value, opts)
server(key).replace(key, data, expiry, flags) && value
end
|
324
325
326
|
# File 'lib/memcache.rb', line 324
def reset
servers.each {|server| server.close if server.respond_to?(:close)}
end
|
#set(key, value, opts = {}) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/memcache.rb', line 136
def set(key, value, opts = {})
opts = compatible_opts(opts)
key = key.to_s
backup.set(key, value, opts) if backup
expiry = opts[:expiry] || default_expiry
flags = opts[:flags] || 0
data = marshal(value, opts)
server(key).set(key, data, expiry, flags)
value
end
|
#stats(field = nil) ⇒ Object
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/memcache.rb', line 328
def stats(field = nil)
if field
servers.collect do |server|
server.stats[field]
end
else
stats = {}
servers.each do |server|
stats[server.name] = server.stats
end
stats
end
end
|
#unlock(key) ⇒ Object
287
288
289
|
# File 'lib/memcache.rb', line 287
def unlock(key)
delete(lock_key(key))
end
|
#update(key, opts = {}) ⇒ Object
219
220
221
222
223
224
225
226
227
|
# File 'lib/memcache.rb', line 219
def update(key, opts = {})
key = key.to_s
value = get(key, :cas => true)
if value
cas(key, yield(value), opts.merge!(:cas => value.memcache_cas))
else
add(key, yield(value), opts)
end
end
|
#with_lock(key, opts = {}) ⇒ Object
291
292
293
294
295
296
297
298
|
# File 'lib/memcache.rb', line 291
def with_lock(key, opts = {})
until lock(key) do
return if opts[:ignore]
sleep(WRITE_LOCK_WAIT) end
yield
unlock(key) unless opts[:keep]
end
|
#write(key, value, opts = nil) ⇒ Object
148
149
150
151
|
# File 'lib/memcache.rb', line 148
def write(key, value, opts = nil)
opts ||= {}
set(key, value, opts.merge(:raw => true))
end
|