Class: Cachetastic::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/cachetastic/cache.rb,
lib/cachetastic/store_object.rb

Overview

When creating a new ‘Cache’ this class should be extended. Once extended you’ll only need to override just the methods that are different for your cache.

class MyAwesomeCache < Cachetastic::Cache
end

MyAwesomeCache.set(1, "One")
MyAwesomeCache.get(1) # => "One"
MyAwesomeCache.update(1, "One!!")
MyAwesomeCache.get(1) # => "One!!"
MyAwesomeCache.delete(1)
MyAwesomeCache.get(1) # => nil

class MyAwesomeCache < Cachetastic::Cache
  class << self
    def get(key)
      super(key) do
        set(key, key * 10)
      end
    end
  end
end

MyAwesomeCache.set(1, "One")
MyAwesomeCache.get(1) # => "One"
MyAwesomeCache.delete(1)
MyAwesomeCache.get(1) # => 10

Defined Under Namespace

Classes: StoreObject

Class Method Summary collapse

Class Method Details

.adapterObject

Returns the underlying Cachetastic::Adapters::Base for this cache.



79
80
81
82
83
84
# File 'lib/cachetastic/cache.rb', line 79

def adapter
  unless @_adapter && @_adapter.valid?
    @_adapter = Cachetastic::Adapters.build(cache_klass)
  end
  @_adapter
end

.cache_klassObject

:nodoc:



92
93
94
# File 'lib/cachetastic/cache.rb', line 92

def cache_klass # :nodoc:
  self
end

.clear_adapter!Object

Clears the adapter so it can be redefined. This is useful if you have reconfigured the cache to use a different adapater, or different settings.



88
89
90
# File 'lib/cachetastic/cache.rb', line 88

def clear_adapter!
  @_adapter = nil
end

.delete(key) ⇒ Object

Deletes an object from the cache.



63
64
65
66
67
68
# File 'lib/cachetastic/cache.rb', line 63

def delete(key)
  do_with_logging(:delete, key) do
    self.adapter.delete(key)
    nil
  end
end

.expire_allObject

Expires all objects for this cache.



71
72
73
74
75
76
# File 'lib/cachetastic/cache.rb', line 71

def expire_all
  do_with_logging(:expire_all, nil) do
    self.adapter.expire_all
    nil
  end
end

.get(key, &block) ⇒ Object

Returns an object from the cache for a given key. If the object comes back as nil and a block is given that block will be run and the results of the block will be returned. This can be used to JIT caches, just make sure in the block to call the set method because the results of the block are not automatically cached.



41
42
43
44
45
46
# File 'lib/cachetastic/cache.rb', line 41

def get(key, &block)
  do_with_logging(:get, key) do
    val = self.adapter.get(key)
    handle_store_object(key, adapter.unmarshal(val), &block)
  end
end

.loggerObject

Returns the Cachetastic::Logger for this cache.



97
98
99
100
101
102
# File 'lib/cachetastic/cache.rb', line 97

def logger
  unless @_logger
    @_logger = Cachetastic::Logger.new(adapter.logger)
  end
  @_logger
end

.set(key, value, expiry_time = nil) ⇒ Object

Set a particular object info the cache for the given key.

An optional third parameter sets the expiry time for the object in the cache. If no expiry_time is passed in then the default expiry_time that has been configured will be used.

If there is an the expiry_swing setting is configured it will be +/- to the expiry time.



56
57
58
59
60
# File 'lib/cachetastic/cache.rb', line 56

def set(key, value, expiry_time = nil)
  do_with_logging(:set, key) do
    self.adapter.set(key, value, calculate_expiry_time(expiry_time))
  end
end