Class: Moneta::Base
- Inherits:
-
Object
- Object
- Moneta::Base
- Includes:
- Mixins::WithOptions
- Defined in:
- lib/moneta/base.rb
Overview
Simple interface to key/value stores with Hash-like interface.
Direct Known Subclasses
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::Riak, Adapters::Sequel, Adapters::Sqlite, Cache, Proxy, Stack
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 value with 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 Mixins::WithOptions
#expires, #prefix, #raw, #with
Instance Method Details
#[](key) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
84 85 86 |
# File 'lib/moneta/base.rb', line 84 def [](key) load(key) end |
#[]=(key, value) ⇒ Object
Store value with key
94 95 96 |
# File 'lib/moneta/base.rb', line 94 def []=(key, value) store(key, value) end |
#close ⇒ Object
Explicitly close the store
51 52 |
# File 'lib/moneta/base.rb', line 51 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.
44 45 46 |
# File 'lib/moneta/base.rb', line 44 def decrement(key, amount = 1, = {}) increment(key, -amount, ) end |
#fetch(key, default = nil, options = nil) ⇒ Object
Fetch value with key
This is a overloaded method:
-
fetch(key, options = {}, &block) retrieve a key. if the key is not available, execute the block and return its return value.
-
fetch(key, value, options = {}) retrieve a key. if the key is not available, return the value.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/moneta/base.rb', line 68 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
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.
29 30 31 |
# File 'lib/moneta/base.rb', line 29 def increment(key, amount = 1, = {}) raise NotImplementedError, 'increment is not supported' end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
13 14 15 |
# File 'lib/moneta/base.rb', line 13 def key?(key, = {}) load(key, ) != nil end |