Class: Moneta::Adapters::Daybreak

Inherits:
Moneta::Adapter show all
Includes:
CreateSupport, DBMAdapter, EachKeySupport, IncrementSupport
Defined in:
lib/moneta/adapters/daybreak.rb

Overview

Daybreak backend

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods included from EachKeySupport

#each_key, included

Methods included from CreateSupport

included

Methods included from IncrementSupport

included

Methods included from DBMAdapter

#close

Methods included from HashAdapter

#clear, #delete, #fetch_values, #key?, #slice, #values_at

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

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 = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :file (String)

    Database file

  • :backend (::Daybreak)

    Use existing backend instance



17
# File 'lib/moneta/adapters/daybreak.rb', line 17

backend { |file:| ::Daybreak::DB.new(file, serializer: ::Daybreak::Serializer::None) }

Instance Method Details

#create(key, value, options = {}) ⇒ Boolean

Note:

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.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



38
39
40
# File 'lib/moneta/adapters/daybreak.rb', line 38

def create(key, value, options = {})
  backend.lock { super }
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

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.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



33
34
35
# File 'lib/moneta/adapters/daybreak.rb', line 33

def increment(key, amount = 1, options = {})
  backend.lock { super }
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Moneta::Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



20
21
22
23
# File 'lib/moneta/adapters/daybreak.rb', line 20

def load(key, options = {})
  backend.load if options[:sync]
  backend[key]
end

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

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.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/moneta/adapters/daybreak.rb', line 43

def merge!(pairs, options = {})
  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

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



26
27
28
29
30
# File 'lib/moneta/adapters/daybreak.rb', line 26

def store(key, value, options = {})
  backend[key] = value
  backend.flush if options[:sync]
  value
end