Module: Moneta::Defaults
- Includes:
- OptionSupport
- Included in:
- Adapters::ActiveRecord, Adapters::ActiveSupportCache, Adapters::Cassandra, Adapters::Client, Adapters::Couch, Adapters::DBM, Adapters::DataMapper, Adapters::Daybreak, Adapters::File, Adapters::Fog, Adapters::GDBM, Adapters::HBase, Adapters::KyotoCabinet, Adapters::LMDB, Adapters::LRUHash, Adapters::LevelDB, Adapters::LocalMemCache, Adapters::MemcachedDalli, Adapters::MemcachedNative, Adapters::Memory, Adapters::MongoBase, Adapters::Null, Adapters::PStore, Adapters::Redis, Adapters::RestClient, Adapters::Riak, Adapters::SDBM, Adapters::Sequel, Adapters::Sqlite, Adapters::TDB, Adapters::TokyoCabinet, 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.
-
#each_key ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#features ⇒ Array<Symbol>
Returns features list.
-
#fetch(key, default = nil, options = nil) ⇒ Object
Fetch a value with a key.
-
#fetch_values(*keys, **options) ⇒ Object
Behaves identically to #values_at except that it accepts an optional block.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#supports?(feature) ⇒ Boolean
Return true if adapter supports the given feature.
-
#update(pairs, options = {}, &block) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Class Method Details
.included(base) ⇒ Object
100 101 102 |
# File 'lib/moneta/mixins.rb', line 100 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
195 196 197 |
# File 'lib/moneta/mixins.rb', line 195 def [](key) load(key) end |
#[]=(key, value) ⇒ Object
Store value with key
205 206 207 |
# File 'lib/moneta/mixins.rb', line 205 def []=(key, value) store(key, value) end |
#close ⇒ Object
Explicitly close the store
154 |
# File 'lib/moneta/mixins.rb', line 154 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.
239 240 241 |
# File 'lib/moneta/mixins.rb', line 239 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.
147 148 149 |
# File 'lib/moneta/mixins.rb', line 147 def decrement(key, amount = 1, = {}) increment(key, -amount, ) 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.
223 224 225 |
# File 'lib/moneta/mixins.rb', line 223 def each_key raise NotImplementedError, 'each_key is not supported' end |
#features ⇒ Array<Symbol>
Returns features list
359 360 361 |
# File 'lib/moneta/mixins.rb', line 359 def features self.class.features end |
#fetch(key, options = {}, &block) ⇒ Object #fetch(key, default, options = {}) ⇒ Object
Fetch a value with a key
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/moneta/mixins.rb', line 179 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 |
#fetch_values(*keys, **options) ⇒ Object #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Behaves identically to #values_at except that it accepts an optional block. When supplied, the block will be called successively with each supplied key that is not present in the store. The return value of the block call will be used in place of nil in returned the array of values.
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/moneta/mixins.rb', line 281 def fetch_values(*keys, **) values = values_at(*keys, **) return values unless block_given? keys.zip(values).map do |key, value| if value == nil yield key else value end 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.
130 131 132 |
# File 'lib/moneta/mixins.rb', line 130 def increment(key, amount = 1, = {}) raise NotImplementedError, 'increment is not supported' end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
113 114 115 |
# File 'lib/moneta/mixins.rb', line 113 def key?(key, = {}) load(key, ) != nil end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.
Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/moneta/mixins.rb', line 340 def merge!(pairs, = {}) pairs.each do |key, value| if block_given? existing = load(key, ) value = yield(key, existing, value) unless existing == nil end store(key, value, ) end self end |
#slice(*keys, **options) ⇒ <(Object, Object)>
The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.
310 311 312 313 314 |
# File 'lib/moneta/mixins.rb', line 310 def slice(*keys, **) keys.zip(values_at(*keys, **)).reject do |_, value| value == nil end end |
#supports?(feature) ⇒ Boolean
Return true if adapter supports the given feature.
366 367 368 |
# File 'lib/moneta/mixins.rb', line 366 def supports?(feature) features.include?(feature) end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.
Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.
352 353 354 |
# File 'lib/moneta/mixins.rb', line 352 def update(pairs, = {}, &block) merge!(pairs, , &block) end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.
Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.
256 257 258 |
# File 'lib/moneta/mixins.rb', line 256 def values_at(*keys, **) keys.map { |key| load(key, ) } end |