Class: Moneta::Adapters::Daybreak
- Inherits:
-
Object
- Object
- Moneta::Adapters::Daybreak
- Includes:
- CreateSupport, DBMAdapter, Defaults, EachKeySupport, IncrementSupport
- Defined in:
- lib/moneta/adapters/daybreak.rb
Overview
Daybreak backend
Instance Attribute Summary
Attributes included from HashAdapter
Instance Method Summary collapse
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ Daybreak
constructor
A new instance of Daybreak.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from EachKeySupport
Methods included from CreateSupport
Methods included from IncrementSupport
Methods included from DBMAdapter
Methods included from HashAdapter
#clear, #delete, #fetch_values, #key?, #slice, #values_at
Methods included from Defaults
#[], #[]=, #close, #decrement, #each_key, #features, #fetch, #fetch_values, included, #key?, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Daybreak
Returns a new instance of Daybreak.
17 18 19 20 21 22 23 |
# File 'lib/moneta/adapters/daybreak.rb', line 17 def initialize( = {}) @backend = [:backend] || begin raise ArgumentError, 'Option :file is required' unless [:file] ::Daybreak::DB.new([:file], serializer: ::Daybreak::Serializer::None) end end |
Instance Method Details
#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.
44 45 46 |
# File 'lib/moneta/adapters/daybreak.rb', line 44 def create(key, value, = {}) @backend.lock { super } 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.
39 40 41 |
# File 'lib/moneta/adapters/daybreak.rb', line 39 def increment(key, amount = 1, = {}) @backend.lock { super } end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
26 27 28 29 |
# File 'lib/moneta/adapters/daybreak.rb', line 26 def load(key, = {}) @backend.load if [:sync] @backend[key] 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 HashAdapter#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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/moneta/adapters/daybreak.rb', line 49 def merge!(pairs, = {}) if block_given? @backend.lock do @backend.update(pairs.map do |key, new_value| new_value = yield(key, load(key), new_value) if key?(key) [key, new_value] end) end else @backend.update(pairs) end self end |
#store(key, value, options = {}) ⇒ Object
Store value with key
32 33 34 35 36 |
# File 'lib/moneta/adapters/daybreak.rb', line 32 def store(key, value, = {}) @backend[key] = value @backend.flush if [:sync] value end |