Module: Moneta::Defaults
- Includes:
- OptionSupport
- Included in:
- Adapters::ActiveRecord, Adapters::Cassandra, Adapters::Client, Adapters::Couch, Adapters::DataMapper, Adapters::File, Adapters::Fog, Adapters::HBase, Adapters::LRUHash, Adapters::LocalMemCache, Adapters::MemcachedDalli, Adapters::MemcachedNative, Adapters::Memory, Adapters::Mongo, Adapters::Null, Adapters::PStore, Adapters::Redis, Adapters::RestClient, Adapters::Riak, Adapters::Sequel, Adapters::Sqlite, Cache, Proxy, Stack
- Defined in:
- lib/moneta/mixins.rb
Overview
Simple interface to key/value stores with Hash-like interface.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch value with key.
-
#[]=(key, value) ⇒ Object
Store value with key.
-
#close ⇒ Object
Explicitly close the store.
-
#decrement(key, amount = 1, options = {}) ⇒ Object
Atomically decrement integer value with key.
-
#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.
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Instance Method Details
#[](key) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
129 130 131 |
# File 'lib/moneta/mixins.rb', line 129 def [](key) load(key) end |
#[]=(key, value) ⇒ Object
Store value with key
139 140 141 |
# File 'lib/moneta/mixins.rb', line 139 def []=(key, value) store(key, value) end |
#close ⇒ Object
Explicitly close the store
93 94 |
# File 'lib/moneta/mixins.rb', line 93 def close 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.
86 87 88 |
# File 'lib/moneta/mixins.rb', line 86 def decrement(key, amount = 1, = {}) increment(key, -amount, ) end |
#fetch(key, options = {}, &block) ⇒ Object #fetch(key, default, options = {}) ⇒ Object
Fetch a value with a key
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/moneta/mixins.rb', line 113 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.
71 72 73 |
# File 'lib/moneta/mixins.rb', line 71 def increment(key, amount = 1, = {}) raise NotImplementedError, 'increment is not supported' end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
56 57 58 |
# File 'lib/moneta/mixins.rb', line 56 def key?(key, = {}) load(key, ) != nil end |