jsierles-memcache-client 1.5.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +98 -0
- data/LICENSE.txt +28 -0
- data/README.txt +54 -0
- data/Rakefile +22 -0
- data/lib/memcache.rb +833 -0
- data/lib/memcache_util.rb +90 -0
- data/test/test_mem_cache.rb +771 -0
- metadata +60 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
##
|
2
|
+
# A utility wrapper around the MemCache client to simplify cache access. All
|
3
|
+
# methods silently ignore MemCache errors.
|
4
|
+
|
5
|
+
module Cache
|
6
|
+
|
7
|
+
##
|
8
|
+
# Returns the object at +key+ from the cache if successful, or nil if either
|
9
|
+
# the object is not in the cache or if there was an error attermpting to
|
10
|
+
# access the cache.
|
11
|
+
#
|
12
|
+
# If there is a cache miss and a block is given the result of the block will
|
13
|
+
# be stored in the cache with optional +expiry+, using the +add+ method rather
|
14
|
+
# than +set+.
|
15
|
+
|
16
|
+
def self.get(key, expiry = 0)
|
17
|
+
start_time = Time.now
|
18
|
+
value = CACHE.get key
|
19
|
+
elapsed = Time.now - start_time
|
20
|
+
ActiveRecord::Base.logger.debug('MemCache Get (%0.6f) %s' % [elapsed, key])
|
21
|
+
if value.nil? and block_given? then
|
22
|
+
value = yield
|
23
|
+
add key, value, expiry
|
24
|
+
end
|
25
|
+
value
|
26
|
+
rescue MemCache::MemCacheError => err
|
27
|
+
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
|
28
|
+
if block_given? then
|
29
|
+
value = yield
|
30
|
+
put key, value, expiry
|
31
|
+
end
|
32
|
+
value
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Sets +value+ in the cache at +key+, with an optional +expiry+ time in
|
37
|
+
# seconds.
|
38
|
+
|
39
|
+
def self.put(key, value, expiry = 0)
|
40
|
+
start_time = Time.now
|
41
|
+
CACHE.set key, value, expiry
|
42
|
+
elapsed = Time.now - start_time
|
43
|
+
ActiveRecord::Base.logger.debug('MemCache Set (%0.6f) %s' % [elapsed, key])
|
44
|
+
value
|
45
|
+
rescue MemCache::MemCacheError => err
|
46
|
+
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Sets +value+ in the cache at +key+, with an optional +expiry+ time in
|
52
|
+
# seconds. If +key+ already exists in cache, returns nil.
|
53
|
+
|
54
|
+
def self.add(key, value, expiry = 0)
|
55
|
+
start_time = Time.now
|
56
|
+
response = CACHE.add key, value, expiry
|
57
|
+
elapsed = Time.now - start_time
|
58
|
+
ActiveRecord::Base.logger.debug('MemCache Add (%0.6f) %s' % [elapsed, key])
|
59
|
+
(response == "STORED\r\n") ? value : nil
|
60
|
+
rescue MemCache::MemCacheError => err
|
61
|
+
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# Deletes +key+ from the cache in +delay+ seconds.
|
67
|
+
|
68
|
+
def self.delete(key, delay = nil)
|
69
|
+
start_time = Time.now
|
70
|
+
CACHE.delete key, delay
|
71
|
+
elapsed = Time.now - start_time
|
72
|
+
ActiveRecord::Base.logger.debug('MemCache Delete (%0.6f) %s' %
|
73
|
+
[elapsed, key])
|
74
|
+
nil
|
75
|
+
rescue MemCache::MemCacheError => err
|
76
|
+
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
|
77
|
+
nil
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# Resets all connections to MemCache servers.
|
82
|
+
|
83
|
+
def self.reset
|
84
|
+
CACHE.reset
|
85
|
+
ActiveRecord::Base.logger.debug 'MemCache Connections Reset'
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|