Class: Cachetastic::Adapters::MemcachePool::Adapter

Inherits:
Base
  • Object
show all
Defined in:
lib/cachetastic/adapters/memcache_pool/adapter.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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Adapter

:nodoc:



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

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

Class Method Details

.connection_digest(adapter) ⇒ Object



115
116
117
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 115

def connection_digest(adapter)
  {:servers => adapter.servers, :mc_options => adapter.mc_options}.to_s.hexdigest
end

.data_connection(adapter) ⇒ Object



119
120
121
122
123
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 119

def data_connection(adapter)
  @_connections_by_klass   ||= {}
  @_connections_by_digest  ||= {}
  return get_connection(adapter, @_connections_by_klass, @_connections_by_digest, nil)
end

.get_connection(adapter, connections_by_class, connections_by_digest, namespace) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 131

def get_connection(adapter, connections_by_class, connections_by_digest, namespace)
  matching_connection = connections_by_class[adapter.klass.name]
  if !matching_connection || !matching_connection.active?
    digest = connection_digest(adapter)
    matching_connection = connections_by_digest[digest]
    if !matching_connection || !matching_connection.active?
      matching_connection = MemCache.new(adapter.servers, adapter.mc_options.merge(:namespace => namespace))
    end
    connections_by_digest[digest] = matching_connection
  end
  connections_by_class[adapter.klass.name] = matching_connection
  matching_connection
end

.ns_connection(adapter) ⇒ Object



125
126
127
128
129
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 125

def ns_connection(adapter)
  @_ns_connections_by_klass   ||= {}
  @_ns_connections_by_digest  ||= {}
  return get_connection(adapter, @_ns_connections_by_klass, @_ns_connections_by_digest, :namespace_versions)
end

.reset_connectionsObject

JDW: TODO: Extract all of this into a MemCachePool class, and add the ability to do true thread-safe pooling



108
109
110
111
112
113
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 108

def reset_connections
  @_connections_by_klass = {}
  @_connections_by_digest = {}
  @_ns_connections_by_klass = {}
  @_ns_connections_by_digest = {}
end

Instance Method Details

#delete(key) ⇒ Object

set



55
56
57
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 55

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

#expire_allObject

delete



59
60
61
62
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 59

def expire_all # :nodoc:
  increment_version
  return nil
end

#get(key) ⇒ Object

:nodoc:



47
48
49
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 47

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

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

get



51
52
53
# File 'lib/cachetastic/adapters/memcache_pool/adapter.rb', line 51

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/memcache_pool/adapter.rb', line 64

def transform_key(key) # :nodoc:
  namespace + ':' + 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/memcache_pool/adapter.rb', line 70

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