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.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.

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
45
46
# File 'lib/cachetastic/adapters/memcached.rb', line 31

def initialize(klass) # :nodoc:
  define_accessor(:mc_servers)
  define_accessor(:mc_options)
  define_accessor(:delete_delay)
  self.delete_delay = 0
  self.mc_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



56
57
58
# File 'lib/cachetastic/adapters/memcached.rb', line 56

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

#expire_allObject

delete



60
61
62
63
64
# File 'lib/cachetastic/adapters/memcached.rb', line 60

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

#get(key) ⇒ Object

:nodoc:



48
49
50
# File 'lib/cachetastic/adapters/memcached.rb', line 48

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

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

get



52
53
54
# File 'lib/cachetastic/adapters/memcached.rb', line 52

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



66
67
68
# File 'lib/cachetastic/adapters/memcached.rb', line 66

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)


72
73
74
75
76
# 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