Class: Moneta::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::WithOptions
Defined in:
lib/moneta/base.rb

Overview

Simple interface to key/value stores with Hash-like interface.

Instance Method Summary collapse

Methods included from Mixins::WithOptions

#expires, #prefix, #raw, #with

Instance Method Details

#[](key) ⇒ Object

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

Parameters:

  • key (Object)

Returns:

  • (Object)

    value



64
65
66
# File 'lib/moneta/base.rb', line 64

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



74
75
76
# File 'lib/moneta/base.rb', line 74

def []=(key, value)
  store(key, value)
end

#closeObject

Explicitly close the store



33
34
# File 'lib/moneta/base.rb', line 33

def close
end

#fetch(key, default = nil, options = nil) ⇒ Object

Fetch value with key

This is a overloaded method:

  • fetch(key, options = {}, &block) retrieve a key. if the key is not available, execute the block and return its return value.

  • fetch(key, value, options = {}) retrieve a key. if the key is not available, return the value.

Parameters:

  • key (Object)
  • default (Object) (defaults to: nil)

    Default value

  • options (Hash) (defaults to: nil)

Returns:

  • (Object)

    value from store



50
51
52
53
54
55
56
57
# File 'lib/moneta/base.rb', line 50

def fetch(key, default = nil, options = nil)
  if block_given?
    raise ArgumentError, 'Only one argument accepted if block is given' if options
    load(key, default || {}) || yield(key)
  else
    load(key, options || {}) || default
  end
end

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

Atomically increment integer value with key

Not every Moneta store implements this method, a NotImplementedError if it is not supported.

Parameters:

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

Returns:

  • (Object)

    value from store

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/moneta/base.rb', line 27

def increment(key, amount = 1, options = {})
  raise NotImplementedError, 'increment is not supported'
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Returns:

  • (Boolean)


13
14
15
# File 'lib/moneta/base.rb', line 13

def key?(key, options = {})
  load(key, options) != nil
end