class Cachetastic::Adapters::Memcached

An adapter to cache objects to the file system.

This adapter supports the following configuration settings, in addition to the default settings:

configatron.cachetastic.defaults.mc_servers = ['127.0.0.1:11211']
configatron.cachetastic.defaults.mc_options = {c_threshold: 10_000,
                                               compression: true,
                                               debug: false,
                                               readonly: false,
                                               urlencode: false}
configatron.cachetastic.delete_delay = 0

The mc_servers setting defines an Array of Memcached mc_servers, represented as “<host>:<port>”.

The mc_options setting is a Hash of settings required by Memcached. See the Memcached documentation for more information on what the settings mean.

The delete_delay setting tells Memcached how long to wait before it deletes the object. This is not the same as expiry_time. It is only used when the delete method is called.

See Cachetastic::Adapters::Base for a list of public API methods.

Public Instance Methods

valid?() click to toggle source

Return false if the connection to Memcached is either nil or not active.

# File lib/cachetastic/adapters/memcached.rb, line 72
def valid?
  return false if @_mc_connection.nil?
  return false unless @_mc_connection.active?
  return true
end

Private Instance Methods

connection() click to toggle source
# File lib/cachetastic/adapters/memcached.rb, line 79
def connection
  unless @_mc_connection && valid? && @_ns_version == get_version
    @_mc_connection = MemCache.new(self.mc_servers, self.mc_options.merge(namespace: namespace))
  end
  @_mc_connection
end
get_version() click to toggle source
# File lib/cachetastic/adapters/memcached.rb, line 99
def get_version
  name = self.klass.name
  v = ns_connection.get(name)
  if v.nil?
    ns_connection.set(name, 1)
    v = 1
  end
  v
end
increment_version() click to toggle source
# File lib/cachetastic/adapters/memcached.rb, line 93
def increment_version
  name = self.klass.name
  v = get_version
  ns_connection.set(name, v + 1)
end
namespace() click to toggle source
# File lib/cachetastic/adapters/memcached.rb, line 109
def namespace
  @_ns_version = get_version
  "#{self.klass.name}.#{@_ns_version}"
end
ns_connection() click to toggle source
# File lib/cachetastic/adapters/memcached.rb, line 86
def ns_connection
  if !@_ns_connection || !@_ns_connection.active?
    @_ns_connection = MemCache.new(self.mc_servers, self.mc_options.merge(namespace: :namespace_versions))
  end
  @_ns_connection
end