mperham-memcache-client 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
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
+ # Try to return a logger object that does not rely
9
+ # on ActiveRecord for logging.
10
+ def self.logger
11
+ @logger ||= if defined? Rails.logger # Rails 2.1 +
12
+ Rails.logger
13
+ elsif defined? RAILS_DEFAULT_LOGGER # Rails 1.2.2 +
14
+ RAILS_DEFAULT_LOGGER
15
+ else
16
+ ActiveRecord::Base.logger # ... very old Rails.
17
+ end
18
+ end
19
+ ##
20
+ # Returns the object at +key+ from the cache if successful, or nil if either
21
+ # the object is not in the cache or if there was an error attermpting to
22
+ # access the cache.
23
+ #
24
+ # If there is a cache miss and a block is given the result of the block will
25
+ # be stored in the cache with optional +expiry+, using the +add+ method rather
26
+ # than +set+.
27
+
28
+ def self.get(key, expiry = 0)
29
+ start_time = Time.now
30
+ value = CACHE.get key
31
+ elapsed = Time.now - start_time
32
+ logger.debug('MemCache Get (%0.6f) %s' % [elapsed, key])
33
+ if value.nil? and block_given? then
34
+ value = yield
35
+ add key, value, expiry
36
+ end
37
+ value
38
+ rescue MemCache::MemCacheError => err
39
+ logger.debug "MemCache Error: #{err.message}"
40
+ if block_given? then
41
+ value = yield
42
+ put key, value, expiry
43
+ end
44
+ value
45
+ end
46
+
47
+ ##
48
+ # Sets +value+ in the cache at +key+, with an optional +expiry+ time in
49
+ # seconds.
50
+
51
+ def self.put(key, value, expiry = 0)
52
+ start_time = Time.now
53
+ CACHE.set key, value, expiry
54
+ elapsed = Time.now - start_time
55
+ logger.debug('MemCache Set (%0.6f) %s' % [elapsed, key])
56
+ value
57
+ rescue MemCache::MemCacheError => err
58
+ ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
59
+ nil
60
+ end
61
+
62
+ ##
63
+ # Sets +value+ in the cache at +key+, with an optional +expiry+ time in
64
+ # seconds. If +key+ already exists in cache, returns nil.
65
+
66
+ def self.add(key, value, expiry = 0)
67
+ start_time = Time.now
68
+ response = CACHE.add key, value, expiry
69
+ elapsed = Time.now - start_time
70
+ logger.debug('MemCache Add (%0.6f) %s' % [elapsed, key])
71
+ (response == "STORED\r\n") ? value : nil
72
+ rescue MemCache::MemCacheError => err
73
+ ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
74
+ nil
75
+ end
76
+
77
+ ##
78
+ # Deletes +key+ from the cache in +delay+ seconds.
79
+
80
+ def self.delete(key, delay = nil)
81
+ start_time = Time.now
82
+ CACHE.delete key, delay
83
+ elapsed = Time.now - start_time
84
+ logger.debug('MemCache Delete (%0.6f) %s' %
85
+ [elapsed, key])
86
+ nil
87
+ rescue MemCache::MemCacheError => err
88
+ logger.debug "MemCache Error: #{err.message}"
89
+ nil
90
+ end
91
+
92
+ ##
93
+ # Resets all connections to MemCache servers.
94
+
95
+ def self.reset
96
+ CACHE.reset
97
+ logger.debug 'MemCache Connections Reset'
98
+ nil
99
+ end
100
+
101
+ end
102
+