# This store uses Memcache as it's backing. # The configuration for this should look something like this: # my_awesome_cache_options: # debug: false # store: memcache # marshall_method: none # default_expiry: <%= 24.hours %> # store_options: # c_threshold: 10_000 # compression: true # debug: false # readonly: false # urlencode: false # logging: # logger_1: # type: file # file: log/memcached.log # servers: # - 127.0.0.1:11211 class Cachetastic::Stores::Memcache < Cachetastic::Stores::Base def setup self.conn = MemCache.new(self.servers, self.store_options.merge({:namespace => self.namespace})) self.version = self.get_version(self.name) end def set(key, value, expiry = 0) self.conn.set(key, value, expiry) end def delete(key, delay = 0) self.conn.delete(key, delay) end def get(key) self.conn.get(key) end def expire_all self.increment_version(self.name) end def inspect self.conn.inspect + " #{self.conn.stats.inspect}" end def valid? begin return (self.conn.active? && self.version == self.get_version(self.name)) rescue Exception => e puts e.message puts e.backtrace.join("\n") return false end end protected attr_accessor :conn attr_accessor :version def namespace v = self.get_version(self.name) return "#{name}.#{v}" end def ns_versions ivar_cache do ns_conn = MemCache.new(self.servers, self.store_options.merge({:namespace => :namespace_versions})) end end def increment_version(name) name = name.to_s v = get_version(name) self.ns_versions.set(name, v + 1) end def get_version(name) name = name.to_s v = self.ns_versions.get(name) if v.nil? self.ns_versions.set(name, 1) v = 1 end v end end