Class: Moneta::Adapters::Daybreak

Inherits:
Memory
  • Object
show all
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 CreateSupport

included

Methods included from IncrementSupport

included

Methods included from HashAdapter

#clear, #delete, #key?

Methods included from Defaults

#[], #[]=, #decrement, #features, #fetch, included, #key?, #supports?

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


11
12
13
14
15
16
17
# File 'lib/moneta/adapters/daybreak.rb', line 11

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

#closeObject

Explicitly close the store

Returns:

  • nil


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

def close
  @backend.close
end

#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

#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