Cache

A utility wrapper around the MemCache client to simplify cache access. All methods silently ignore MemCache errors.

This API is deprecated, please use the Rails.cache API or your own wrapper API around MemCache.

Public Class Methods

add(key, value, expiry = 0) click to toggle source

Sets value in the cache at key, with an optional expiry time in seconds. If key already exists in cache, returns nil.

    # File lib/memcache_util.rb, line 69
69:   def self.add(key, value, expiry = 0)
70:     start_time = Time.now
71:     response = CACHE.add key, value, expiry
72:     elapsed = Time.now - start_time
73:     logger.debug('MemCache Add (%0.6f)  %s' % [elapsed, key])
74:     (response == "STORED\r\n") ? value : nil
75:   rescue MemCache::MemCacheError => err
76:     ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
77:     nil
78:   end
delete(key, delay = nil) click to toggle source

Deletes key from the cache in delay seconds.

    # File lib/memcache_util.rb, line 83
83:   def self.delete(key, delay = nil)
84:     start_time = Time.now
85:     CACHE.delete key, delay
86:     elapsed = Time.now - start_time
87:     logger.debug('MemCache Delete (%0.6f)  %s' %
88:                                     [elapsed, key])
89:     nil
90:   rescue MemCache::MemCacheError => err
91:     logger.debug "MemCache Error: #{err.message}"
92:     nil
93:   end
get(key, expiry = 0) click to toggle source

Returns the object at key from the cache if successful, or nil if either the object is not in the cache or if there was an error attermpting to access the cache.

If there is a cache miss and a block is given the result of the block will be stored in the cache with optional expiry, using the add method rather than set.

    # File lib/memcache_util.rb, line 31
31:   def self.get(key, expiry = 0)
32:     start_time = Time.now
33:     value = CACHE.get key
34:     elapsed = Time.now - start_time
35:     logger.debug('MemCache Get (%0.6f)  %s' % [elapsed, key])
36:     if value.nil? and block_given? then
37:       value = yield
38:       add key, value, expiry
39:     end
40:     value
41:   rescue MemCache::MemCacheError => err
42:     logger.debug "MemCache Error: #{err.message}"
43:     if block_given? then
44:       value = yield
45:       put key, value, expiry
46:     end
47:     value
48:   end
logger() click to toggle source

Try to return a logger object that does not rely on ActiveRecord for logging.

    # File lib/memcache_util.rb, line 13
13:   def self.logger
14:     @logger ||= if defined? Rails.logger # Rails 2.1 +
15:       Rails.logger
16:     elsif defined? RAILS_DEFAULT_LOGGER # Rails 1.2.2 +
17:       RAILS_DEFAULT_LOGGER
18:     else
19:       ActiveRecord::Base.logger # ... very old Rails.
20:     end
21:   end
put(key, value, expiry = 0) click to toggle source

Sets value in the cache at key, with an optional expiry time in seconds.

    # File lib/memcache_util.rb, line 54
54:   def self.put(key, value, expiry = 0)
55:     start_time = Time.now
56:     CACHE.set key, value, expiry
57:     elapsed = Time.now - start_time
58:     logger.debug('MemCache Set (%0.6f)  %s' % [elapsed, key])
59:     value
60:   rescue MemCache::MemCacheError => err
61:     ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
62:     nil
63:   end
reset() click to toggle source

Resets all connections to MemCache servers.

     # File lib/memcache_util.rb, line 98
 98:   def self.reset
 99:     CACHE.reset
100:     logger.debug 'MemCache Connections Reset'
101:     nil
102:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.