Class: Cachetastic::Adapters::Base

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

Overview

This class should be extended to create new adapters for various backends. It is important that all subclasses call the initialize method in this base, otherwise things just will not work right.

This base class provides common functionality and an API for all adapters to be used with Cachetastic.

The default settings for all adapters are:

configatron.cachetastic.defaults.marshal_method = :none
configatron.cachetastic.defaults.expiry_swing = 0
configatron.cachetastic.defaults.default_expiry = 86400
configatron.cachetastic.defaults.debug = true
configatron.cachetastic.defaults.adapter = Cachetastic::Adapters::LocalMemory
logger = ::Logger.new(File.join(FileUtils.pwd, 'log', 'cachetastic.log'))
logger.level = ::Logger::DEBUG
configatron.cachetastic.defaults.logger = logger

See the README for more information on what each of those settings mean, and what are values may be used for each one.

Direct Known Subclasses

Dalli, File, LocalMemory, Memcached, Mongoid, Redis

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Base

Creates a new adapter. It takes a class reference to tie the instance of the adapter to a particular class. Note that it is a class reference and not an instance reference.

Examples:

Cachetastic::Adapters::Base.new(User)

Adapters are configured using the Configatron gem.

Examples:

configatron.cachetastic.user.adapter = Cachetastic::Adapters::File
configatron.cachetastic.user.expiry_time = 5.hours
configatron.cachetastic.defaults.expiry_time = 24.hours

Refered to each adapter for its specific configuration settings.


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cachetastic/adapters/base.rb', line 67

def initialize(klass)
  self.klass = klass
  configatron.cachetastic.defaults.configatron_keys.each do |key|
    define_accessor(key)
    self.send("#{key}=", configatron.cachetastic.defaults.send(key))
  end
  klass.to_configatron(:cachetastic).configatron_keys.each do |key|
    define_accessor(key)
    self.send("#{key}=", klass.to_configatron(:cachetastic).send(key))
  end
end

Instance Attribute Details

#klassObject

The Class that this adapter is associated with. Note that it is a class reference and not an instance reference.


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

def klass
  @klass
end

Instance Method Details

#debug?Boolean

:nodoc:

Returns:

  • (Boolean)

135
136
137
138
# File 'lib/cachetastic/adapters/base.rb', line 135

def debug? # :nodoc:
  return self.debug if self.respond_to?(:debug)
  return false
end

#delete(key) ⇒ Object

This method MUST be implemented by a subclass!

The implementation of this method should take a key and remove an object, if it exists, from an underlying persistence store.

Raises:

  • (NoMethodError)

101
102
103
# File 'lib/cachetastic/adapters/base.rb', line 101

def delete(key)
  raise NoMethodError.new('delete')
end

#expire_allObject

This method MUST be implemented by a subclass!

The implementation of this method is expected to delete all objects belonging to the associated cache from the underlying persistence store. It is NOT meant to delete ALL objects across ALL caches for the underlying persistence store. That would be very very bad!!

Raises:

  • (NoMethodError)

112
113
114
# File 'lib/cachetastic/adapters/base.rb', line 112

def expire_all
  raise NoMethodError.new('expire_all')
end

#get(key) ⇒ Object

This method MUST be implemented by a subclass!

The implementation of this method should take a key and return an associated object, if available, from the underlying persistence layer.

Raises:

  • (NoMethodError)

84
85
86
# File 'lib/cachetastic/adapters/base.rb', line 84

def get(key)
  raise NoMethodError.new('get')
end

#marshal(value) ⇒ Object

:nodoc:


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cachetastic/adapters/base.rb', line 140

def marshal(value) # :nodoc:
  return nil if value.nil?
  case self.marshal_method.to_sym
  when :yaml
    return YAML.dump(value)
  when :ruby
    return Marshal.dump(value)
  else
    return value
  end
end

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

This method MUST be implemented by a subclass!

The implementation of this method should take a key, a value, and an expiry time and save it to the persistence store, where it should live until it is either deleted by the user of the expiry time has passed.

Raises:

  • (NoMethodError)

93
94
95
# File 'lib/cachetastic/adapters/base.rb', line 93

def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry)
  raise NoMethodError.new('set')
end

#transform_key(key) ⇒ Object

Allows an adapter to transform the key to a safe representation for it's backend. For example, the key: '$*…123()%~q' is not a key for the file system, so the Cachetastic::Adapters::File class should override this to make it safe for the file system.


122
123
124
# File 'lib/cachetastic/adapters/base.rb', line 122

def transform_key(key)
  key
end

#unmarshal(value) ⇒ Object

:nodoc:


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cachetastic/adapters/base.rb', line 152

def unmarshal(value) # :nodoc:
  return nil if value.nil?
  case self.marshal_method.to_sym
  when :yaml
    return YAML.load(value)
  when :ruby
    return Marshal.load(value)
  else
    return value
  end
end

#valid?Boolean

This method MUST be implemented by a subclass!

The implementation of this method should return true if the adapter is in a valid state, and false if it is not.

Returns:

  • (Boolean)

131
132
133
# File 'lib/cachetastic/adapters/base.rb', line 131

def valid?
  true
end