/usr/lib/ruby/1.8/memcache_util.rb is in libmemcache-client-ruby1.8 1.7.8-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | ##
# 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.
module Cache
##
# Try to return a logger object that does not rely
# on ActiveRecord for logging.
def self.logger
@logger ||= if defined? Rails.logger # Rails 2.1 +
Rails.logger
elsif defined? RAILS_DEFAULT_LOGGER # Rails 1.2.2 +
RAILS_DEFAULT_LOGGER
else
ActiveRecord::Base.logger # ... very old Rails.
end
end
##
# 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+.
def self.get(key, expiry = 0)
start_time = Time.now
value = CACHE.get key
elapsed = Time.now - start_time
logger.debug('MemCache Get (%0.6f) %s' % [elapsed, key])
if value.nil? and block_given? then
value = yield
add key, value, expiry
end
value
rescue MemCache::MemCacheError => err
logger.debug "MemCache Error: #{err.message}"
if block_given? then
value = yield
put key, value, expiry
end
value
end
##
# Sets +value+ in the cache at +key+, with an optional +expiry+ time in
# seconds.
def self.put(key, value, expiry = 0)
start_time = Time.now
CACHE.set key, value, expiry
elapsed = Time.now - start_time
logger.debug('MemCache Set (%0.6f) %s' % [elapsed, key])
value
rescue MemCache::MemCacheError => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
nil
end
##
# Sets +value+ in the cache at +key+, with an optional +expiry+ time in
# seconds. If +key+ already exists in cache, returns nil.
def self.add(key, value, expiry = 0)
start_time = Time.now
response = CACHE.add key, value, expiry
elapsed = Time.now - start_time
logger.debug('MemCache Add (%0.6f) %s' % [elapsed, key])
(response == "STORED\r\n") ? value : nil
rescue MemCache::MemCacheError => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
nil
end
##
# Deletes +key+ from the cache in +delay+ seconds.
def self.delete(key, delay = nil)
start_time = Time.now
CACHE.delete key, delay
elapsed = Time.now - start_time
logger.debug('MemCache Delete (%0.6f) %s' %
[elapsed, key])
nil
rescue MemCache::MemCacheError => err
logger.debug "MemCache Error: #{err.message}"
nil
end
##
# Resets all connections to MemCache servers.
def self.reset
CACHE.reset
logger.debug 'MemCache Connections Reset'
nil
end
end
|