Class: Moneta::Adapters::Daybreak

Inherits:
Object
  • Object
show all
Includes:
CreateSupport, DBMAdapter, Defaults, EachKeySupport, IncrementSupport
Defined in:
lib/moneta/adapters/daybreak.rb

Overview

Daybreak backend

Instance Attribute Summary

Attributes included from HashAdapter

#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 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

Parameters:

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

Options Hash (options):

  • :file (String)

    Database file

  • :backend (::Daybreak)

    Use existing backend instance



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

def initialize(options = {})
  @backend = options[:backend] ||
    begin
      raise ArgumentError, 'Option :file is required' unless options[:file]
      ::Daybreak::DB.new(options[:file], serializer: ::Daybreak::Serializer::None)
    end
end

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



44
45
46
# File 'lib/moneta/adapters/daybreak.rb', line 44

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



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

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



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

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)


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, 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



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

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