Module: Moneta::Defaults
- Includes:
- OptionSupport
- Included in:
- Adapters::ActiveRecord, Adapters::Cassandra, Adapters::Client, Adapters::Couch, Adapters::DataMapper, Adapters::File, Adapters::Fog, Adapters::HBase, Adapters::LMDB, Adapters::LRUHash, Adapters::LocalMemCache, Adapters::MemcachedDalli, Adapters::MemcachedNative, Adapters::Memory, Adapters::MongoBase, Adapters::Null, Adapters::PStore, Adapters::Redis, Adapters::RestClient, Adapters::Riak, Adapters::Sequel, Adapters::Sqlite, Adapters::TokyoTyrant, Cache, Proxy, Stack
- Defined in:
- lib/moneta/mixins.rb
Overview
Simple interface to key/value stores with Hash-like interface.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch value with key.
-
#[]=(key, value) ⇒ Object
Store value with key.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#decrement(key, amount = 1, options = {}) ⇒ Object
Atomically decrement integer value with key.
-
#features ⇒ Array<Symbol>
Returns features list.
-
#fetch(key, default = nil, options = nil) ⇒ Object
Fetch a value with a key.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#supports?(feature) ⇒ Boolean
Return true if adapter supports the given feature.
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Class Method Details
.included(base) ⇒ Object
81 82 83 |
# File 'lib/moneta/mixins.rb', line 81 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#[](key) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
177 178 179 |
# File 'lib/moneta/mixins.rb', line 177 def [](key) load(key) end |
#[]=(key, value) ⇒ Object
Store value with key
187 188 189 |
# File 'lib/moneta/mixins.rb', line 187 def []=(key, value) store(key, value) end |
#close ⇒ Object
Explicitly close the store
135 136 |
# File 'lib/moneta/mixins.rb', line 135 def 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.
203 204 205 |
# File 'lib/moneta/mixins.rb', line 203 def create(key, value, = {}) raise NotImplementedError, 'create is not supported' end |
#decrement(key, amount = 1, options = {}) ⇒ Object
Atomically decrement integer value with key
This is just syntactic sugar for calling #increment with a negative value.
This method also accepts negative amounts.
128 129 130 |
# File 'lib/moneta/mixins.rb', line 128 def decrement(key, amount = 1, = {}) increment(key, -amount, ) end |
#features ⇒ Array<Symbol>
Returns features list
210 211 212 |
# File 'lib/moneta/mixins.rb', line 210 def features self.class.features end |
#fetch(key, options = {}, &block) ⇒ Object #fetch(key, default, options = {}) ⇒ Object
Fetch a value with a key
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/moneta/mixins.rb', line 161 def fetch(key, default = nil, = nil) if block_given? raise ArgumentError, 'Only one argument accepted if block is given' if result = load(key, default || {}) result == nil ? yield(key) : result else result = load(key, || {}) result == nil ? default : result end 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.
111 112 113 |
# File 'lib/moneta/mixins.rb', line 111 def increment(key, amount = 1, = {}) raise NotImplementedError, 'increment is not supported' end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
94 95 96 |
# File 'lib/moneta/mixins.rb', line 94 def key?(key, = {}) load(key, ) != nil end |
#supports?(feature) ⇒ Boolean
Return true if adapter supports the given feature.
217 218 219 |
# File 'lib/moneta/mixins.rb', line 217 def supports?(feature) features.include?(feature) end |