Class: Moneta::Cache
Overview
Combines two stores. One is used as cache, the other as backend adapter.
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.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#each_key(&block) ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#features ⇒ Array<Symbol>
Returns features list.
-
#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, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
Instance Attribute Details
#adapter ⇒ Object
38 39 40 |
# File 'lib/moneta/cache.rb', line 38 def adapter @adapter 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
92 93 94 95 96 |
# File 'lib/moneta/cache.rb', line 92 def clear( = {}) @cache.clear() @adapter.clear() self end |
#close ⇒ Object
Explicitly close the store
99 100 101 102 |
# File 'lib/moneta/cache.rb', line 99 def close @cache.close @adapter.close end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
76 77 78 79 80 81 82 83 |
# File 'lib/moneta/cache.rb', line 76 def create(key, value, = {}) if @adapter.create(key, value, ) @cache.store(key, value, ) true else false end end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
86 87 88 89 |
# File 'lib/moneta/cache.rb', line 86 def delete(key, = {}) @cache.delete(key, ) @adapter.delete(key, ) end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.
105 106 107 108 109 110 111 112 |
# File 'lib/moneta/cache.rb', line 105 def each_key(&block) raise NotImplementedError, 'adapter doesn\'t support #each_key' \ unless supports? :each_key return enum_for(:each_key) unless block_given? @adapter.each_key(&block) self end |
#features ⇒ Array<Symbol>
Returns features list
115 116 117 |
# File 'lib/moneta/cache.rb', line 115 def features @features ||= ((@cache.features + [:create, :increment, :each_key]) & @adapter.features).freeze 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.
70 71 72 73 |
# File 'lib/moneta/cache.rb', line 70 def increment(key, amount = 1, = {}) @cache.delete(key, ) @adapter.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, ) || @adapter.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 |
# File 'lib/moneta/cache.rb', line 55 def load(key, = {}) if [:sync] || (value = @cache.load(key, )) == nil value = @adapter.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, ) @adapter.store(key, value, ) end |