class Cachetastic::Adapters::Base
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.
Attributes
The Class that this adapter is associated with. Note that it is a class reference and not an instance reference.
Public Class Methods
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.
# 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
Public Instance Methods
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.
# File lib/cachetastic/adapters/base.rb, line 101 def delete(key) raise NoMethodError.new('delete') end
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!!
# File lib/cachetastic/adapters/base.rb, line 112 def expire_all raise NoMethodError.new('expire_all') end
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.
# File lib/cachetastic/adapters/base.rb, line 84 def get(key) raise NoMethodError.new('get') end
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.
# File lib/cachetastic/adapters/base.rb, line 93 def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) raise NoMethodError.new('set') end
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.
# File lib/cachetastic/adapters/base.rb, line 122 def transform_key(key) key end
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.
# File lib/cachetastic/adapters/base.rb, line 131 def valid? true end
Private Instance Methods
# File lib/cachetastic/adapters/base.rb, line 165 def define_accessor(key) instance_eval(%{ def #{key} @#{key} end def #{key}=(x) @#{key} = x end }) end