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
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 = {}) {|Builder| ... } ⇒ 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
38 39 40 |
# File 'lib/moneta/cache.rb', line 38 def backend @backend end |
#cache ⇒ Object
38 39 40 |
# File 'lib/moneta/cache.rb', line 38 def cache @cache end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
83 84 85 86 87 |
# File 'lib/moneta/cache.rb', line 83 def clear( = {}) @cache.clear() @backend.clear() self end |
#close ⇒ Object
Explicitly close the store
90 91 92 93 |
# File 'lib/moneta/cache.rb', line 90 def close @cache.close @backend.close end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
77 78 79 80 |
# File 'lib/moneta/cache.rb', line 77 def delete(key, = {}) @cache.delete(key, ) @backend.delete(key, ) end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
71 72 73 74 |
# File 'lib/moneta/cache.rb', line 71 def increment(key, amount = 1, = {}) @cache.delete(key, ) @backend.increment(key, amount, ) end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
50 51 52 |
# File 'lib/moneta/cache.rb', line 50 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
55 56 57 58 59 60 61 62 |
# File 'lib/moneta/cache.rb', line 55 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
65 66 67 68 |
# File 'lib/moneta/cache.rb', line 65 def store(key, value, = {}) @cache.store(key, value, ) @backend.store(key, value, ) end |