Class: Cachetastic::Adapters::Memcached

Inherits:
Base
  • Object
show all
Defined in:
lib/cachetastic/adapters/memcached.rb

Overview

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.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 servers setting defines an Array of Mecached 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.

Instance Attribute Summary

Attributes inherited from Base

#klass

Instance Method Summary collapse

Methods inherited from Base

#debug?, #marshal, #unmarshal

Constructor Details

#initialize(klass) ⇒ Memcached

:nodoc:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cachetastic/adapters/memcached.rb', line 31

def initialize(klass) # :nodoc:
  define_accessor(:servers)
  define_accessor(:mc_options)
  define_accessor(:delete_delay)
  self.delete_delay = 0
  self.servers = ['127.0.0.1:11211']
  self.mc_options = {:c_threshold => 10_000,
                     :compression => true,
                     :debug => false,
                     :readonly => false,
                     :urlencode => false}
  super
  connection
end

Instance Method Details

#delete(key) ⇒ Object

set



54
55
56
# File 'lib/cachetastic/adapters/memcached.rb', line 54

def delete(key) # :nodoc:
  connection.delete(transform_key(key), self.delete_delay)
end

#expire_allObject

delete



58
59
60
61
62
# File 'lib/cachetastic/adapters/memcached.rb', line 58

def expire_all # :nodoc:
  increment_version
  @_mc_connection = nil
  return nil
end

#get(key) ⇒ Object

:nodoc:



46
47
48
# File 'lib/cachetastic/adapters/memcached.rb', line 46

def get(key) # :nodoc:
  connection.get(transform_key(key), false)
end

#set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) ⇒ Object

get



50
51
52
# File 'lib/cachetastic/adapters/memcached.rb', line 50

def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) # :nodoc:
  connection.set(transform_key(key), marshal(value), expiry_time, false)
end

#transform_key(key) ⇒ Object

expire_all



64
65
66
# File 'lib/cachetastic/adapters/memcached.rb', line 64

def transform_key(key) # :nodoc:
  key.to_s.hexdigest
end

#valid?Boolean

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

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/cachetastic/adapters/memcached.rb', line 70

def valid?
  return false if @_mc_connection.nil?
  return false unless @_mc_connection.active?
  return true
end