Class: Cachetastic::Cache
- Inherits:
-
Object
- Object
- Cachetastic::Cache
- Includes:
- Singleton
- 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
def get(key)
super(key) do
set(key, key * 10)
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
Instance Method Summary collapse
-
#adapter ⇒ Object
Returns the underlying Cachetastic::Adapters::Base for this cache.
-
#cache_klass ⇒ Object
:nodoc:.
-
#clear_adapter! ⇒ Object
Clears the adapter so it can be redefined.
-
#delete(key) ⇒ Object
Deletes an object from the cache.
-
#expire_all ⇒ Object
Expires all objects for this cache.
-
#get(key, &block) ⇒ Object
Returns an object from the cache for a given key.
-
#logger ⇒ Object
Returns the Cachetastic::Logger for this cache.
-
#set(key, value, expiry_time = nil) ⇒ Object
Set a particular object info the cache for the given key.
-
#to_configatron(*args) ⇒ Object
:nodoc:.
Class Method Details
.available_caches ⇒ Object
38 39 40 |
# File 'lib/cachetastic/cache.rb', line 38 def available_caches @available_caches ||= [] end |
.inherited(klass) ⇒ Object
42 43 44 45 |
# File 'lib/cachetastic/cache.rb', line 42 def inherited(klass) available_caches << klass super end |
.method_missing(sym, *args, &block) ⇒ Object
34 35 36 |
# File 'lib/cachetastic/cache.rb', line 34 def method_missing(sym, *args, &block) self.instance.send(sym, *args, &block) end |
Instance Method Details
#adapter ⇒ Object
Returns the underlying Cachetastic::Adapters::Base for this cache.
101 102 103 104 105 106 |
# File 'lib/cachetastic/cache.rb', line 101 def adapter unless @_adapter && @_adapter.valid? @_adapter = Cachetastic::Adapters.build(cache_klass) end @_adapter end |
#cache_klass ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/cachetastic/cache.rb', line 114 def cache_klass # :nodoc: self.class 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.
110 111 112 |
# File 'lib/cachetastic/cache.rb', line 110 def clear_adapter! @_adapter = nil end |
#delete(key) ⇒ Object
Deletes an object from the cache.
81 82 83 84 85 86 87 88 |
# File 'lib/cachetastic/cache.rb', line 81 def delete(key) do_with_logging(:delete, key) do retryable do self.adapter.delete(key) nil end end end |
#expire_all ⇒ Object
Expires all objects for this cache.
91 92 93 94 95 96 97 98 |
# File 'lib/cachetastic/cache.rb', line 91 def expire_all do_with_logging(:expire_all, nil) do retryable do self.adapter.expire_all nil end 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.
55 56 57 58 59 60 61 62 |
# File 'lib/cachetastic/cache.rb', line 55 def get(key, &block) do_with_logging(:get, key) do retryable do val = self.adapter.get(key) handle_store_object(key, adapter.unmarshal(val), &block) end end end |
#logger ⇒ Object
Returns the Cachetastic::Logger for this cache.
119 120 121 122 123 124 |
# File 'lib/cachetastic/cache.rb', line 119 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.
72 73 74 75 76 77 78 |
# File 'lib/cachetastic/cache.rb', line 72 def set(key, value, expiry_time = nil) do_with_logging(:set, key) do retryable do self.adapter.set(key, adapter.marshal(value), calculate_expiry_time(expiry_time)) end end end |
#to_configatron(*args) ⇒ Object
:nodoc:
126 127 128 |
# File 'lib/cachetastic/cache.rb', line 126 def to_configatron(*args) # :nodoc: self.class.to_configatron(*args) end |