Class: Moneta::Cache
Overview
Combines two stores. One is used as cache, the other as backend.
Defined Under Namespace
Classes: DSL
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
- #cache ⇒ Object readonly
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}, &block) ⇒ Cache
constructor
A new instance of Cache.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #decrement, #fetch
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
Instance Attribute Details
#backend ⇒ Object (readonly)
42 43 44 |
# File 'lib/moneta/cache.rb', line 42 def backend @backend end |
#cache ⇒ Object (readonly)
42 43 44 |
# File 'lib/moneta/cache.rb', line 42 def cache @cache end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
82 83 84 85 86 |
# File 'lib/moneta/cache.rb', line 82 def clear( = {}) @cache.clear() @backend.clear() self end |
#close ⇒ Object
Explicitly close the store
89 90 91 92 |
# File 'lib/moneta/cache.rb', line 89 def close @cache.close @backend.close end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
76 77 78 79 |
# File 'lib/moneta/cache.rb', line 76 def delete(key, = {}) @cache.delete(key, ) @backend.delete(key, ) end |
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key
Not every Moneta store implements this method, a NotImplementedError if it is not supported.
This method also accepts negative amounts.
70 71 72 73 |
# File 'lib/moneta/cache.rb', line 70 def increment(key, amount = 1, = {}) @cache.delete(key, ) @backend.increment(key, amount, ) end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
49 50 51 |
# File 'lib/moneta/cache.rb', line 49 def key?(key, = {}) @cache.key?(key, ) || @backend.key?(key, ) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
54 55 56 57 58 59 60 61 |
# File 'lib/moneta/cache.rb', line 54 def load(key, = {}) value = @cache.load(key, ) if value == nil value = @backend.load(key, ) @cache.store(key, value, ) if value != nil end value end |
#store(key, value, options = {}) ⇒ Object
Store value with key
64 65 66 67 |
# File 'lib/moneta/cache.rb', line 64 def store(key, value, = {}) @cache.store(key, value, ) @backend.store(key, value, ) end |