Module: Moneta::Defaults

Overview

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

Instance Method Summary collapse

Methods included from OptionSupport

#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



139
140
141
# File 'lib/moneta/mixins.rb', line 139

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



149
150
151
# File 'lib/moneta/mixins.rb', line 149

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

#closeObject

Explicitly close the store

Returns:

  • nil



103
104
# File 'lib/moneta/mixins.rb', line 103

def close
end

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

Atomically decrement integer value with key

This is just syntactic sugar for calling #increment with a negative value.

This method also accepts negative amounts.

Parameters:

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

Returns:

  • (Object)

    value from store



96
97
98
# File 'lib/moneta/mixins.rb', line 96

def decrement(key, amount = 1, options = {})
  increment(key, -amount, options)
end

#fetch(key, options = {}, &block) ⇒ Object #fetch(key, default, options = {}) ⇒ Object

Fetch a value with a key

Overloads:

  • #fetch(key, options = {}, &block) ⇒ Object

    retrieve a key. if the key is not available, execute the block and return its return value.

    Parameters:

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

    Returns:

    • (Object)

      value from store

  • #fetch(key, default, options = {}) ⇒ Object

    retrieve a key. if the key is not available, return the default value.

    Parameters:

    • key (Object)
    • default (Object)

      Default value

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

    Returns:

    • (Object)

      value from store



123
124
125
126
127
128
129
130
131
132
# File 'lib/moneta/mixins.rb', line 123

def fetch(key, default = nil, options = nil)
  if block_given?
    raise ArgumentError, 'Only one argument accepted if block is given' if options
    result = load(key, default || {})
    result == nil ? yield(key) : result
  else
    result = load(key, options || {})
    result == nil ? default : result
  end
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: {})

Returns:

  • (Object)

    value from store

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/moneta/mixins.rb', line 81

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: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


66
67
68
# File 'lib/moneta/mixins.rb', line 66

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