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, &block) ⇒ Object
-
#get_or_set(key, *args, &block) ⇒ 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
Returns a new instance of 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
#backup ⇒ Object
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
|
#servers ⇒ Object
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/memcache.rb', line 356
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
#[](key) ⇒ Object
348
349
350
|
# File 'lib/memcache.rb', line 348
def [](key)
get(key)
end
|
#[]=(key, value) ⇒ Object
352
353
354
|
# File 'lib/memcache.rb', line 352
def []=(key, value)
set(key, value)
end
|
#add(key, value, opts = {}) ⇒ Object
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/memcache.rb', line 156
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
255
256
257
258
|
# File 'lib/memcache.rb', line 255
def add_or_get(key, value, opts = {})
add(key, value, opts) || get(key, opts)
end
|
#append(key, value) ⇒ Object
189
190
191
192
193
|
# File 'lib/memcache.rb', line 189
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
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/memcache.rb', line 178
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
|
#clone ⇒ Object
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
201
202
203
204
|
# File 'lib/memcache.rb', line 201
def count(key)
value = get(key, :raw => true)
value.to_i if value
end
|
#decr(key, amount = 1) ⇒ Object
214
215
216
217
218
219
220
|
# File 'lib/memcache.rb', line 214
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
312
313
314
315
316
|
# File 'lib/memcache.rb', line 312
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
318
319
320
321
322
323
324
325
326
|
# File 'lib/memcache.rb', line 318
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
126
127
128
|
# 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]
result = server(key).gets(key)
cas(key, result[:value], :raw => true, :cas => result[:cas], :expiry => opts[:expiry]) if result
else
result = server(key).get(key, opts[:cas])
end
if result
result[:value] = unmarshal(result[:value], key) unless opts[:raw]
opts[:meta] ? result : result[:value]
elsif backup
backup.get(key, opts)
end
end
end
|
#get_or_add(key, *args, &block) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/memcache.rb', line 232
def get_or_add(key, *args, &block)
key = key.to_s
if block
opts = args[0] || {}
else
opts = args[1] || {}
block = lambda { args[0] }
end
get(key, opts) || add(key, block.call(), opts) || get(key, opts)
end
|
#get_or_set(key, *args, &block) ⇒ Object
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/memcache.rb', line 244
def get_or_set(key, *args, &block)
key = key.to_s
if block_given?
opts = args[0] || {}
else
opts = args[1] || {}
block = lambda { args[0] }
end
get(key, opts) || set(key, block.call(), opts)
end
|
#get_some(keys, opts = {}) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/memcache.rb', line 260
def get_some(keys, opts = {})
keys = keys.collect {|key| key.to_s}
results = opts[:disable] ? {} : self.multi_get(keys, opts)
if opts[:validation]
results.delete_if do |key, result|
value = opts[:meta] ? result[:value] : result
not opts[:validation].call(key, value)
end
end
keys_to_fetch = keys - results.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
results[key] = opts[:meta] ? {:value => value} : value
end
end
results
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
206
207
208
209
210
211
212
|
# File 'lib/memcache.rb', line 206
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
|
#inspect ⇒ Object
82
83
84
|
# File 'lib/memcache.rb', line 82
def inspect
"<Memcache: %d servers, ns: %p>" % [@servers.length, namespace]
end
|
#lock(key, opts = {}) ⇒ Object
285
286
287
288
289
|
# File 'lib/memcache.rb', line 285
def lock(key, opts = {})
expiry = opts[:expiry] || LOCK_TIMEOUT
add(lock_key(key), Socket.gethostname, :expiry => expiry, :raw => true)
end
|
#lock_key(key) ⇒ Object
304
305
306
|
# File 'lib/memcache.rb', line 304
def lock_key(key)
"lock:#{key}"
end
|
#locked?(key) ⇒ Boolean
308
309
310
|
# File 'lib/memcache.rb', line 308
def locked?(key)
get(lock_key(key), :raw => true)
end
|
#prepend(key, value) ⇒ Object
195
196
197
198
199
|
# File 'lib/memcache.rb', line 195
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
130
131
132
133
|
# File 'lib/memcache.rb', line 130
def read(key, opts = nil)
opts ||= {}
get(key, opts.merge(:raw => true))
end
|
#read_multi(*keys) ⇒ Object
135
136
137
|
# File 'lib/memcache.rb', line 135
def read_multi(*keys)
get(keys)
end
|
#replace(key, value, opts = {}) ⇒ Object
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/memcache.rb', line 167
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
|
#reset ⇒ Object
328
329
330
|
# File 'lib/memcache.rb', line 328
def reset
servers.each {|server| server.close if server.respond_to?(:close)}
end
|
#set(key, value, opts = {}) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/memcache.rb', line 139
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
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/memcache.rb', line 332
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
291
292
293
|
# File 'lib/memcache.rb', line 291
def unlock(key)
delete(lock_key(key))
end
|
#update(key, opts = {}) ⇒ Object
222
223
224
225
226
227
228
229
230
|
# File 'lib/memcache.rb', line 222
def update(key, opts = {})
key = key.to_s
result = get(key, :cas => true, :meta => true)
if result
cas(key, yield(result[:value]), opts.merge!(:cas => result[:cas]))
else
add(key, yield(result[:value]), opts)
end
end
|
#with_lock(key, opts = {}) ⇒ Object
295
296
297
298
299
300
301
302
|
# File 'lib/memcache.rb', line 295
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
151
152
153
154
|
# File 'lib/memcache.rb', line 151
def write(key, value, opts = nil)
opts ||= {}
set(key, value, opts.merge(:raw => true))
end
|