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



84
85
86
# File 'lib/moneta/base.rb', line 84

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



94
95
96
# File 'lib/moneta/base.rb', line 94

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

#closeObject

Explicitly close the store

Returns:

  • nil



51
52
# File 'lib/moneta/base.rb', line 51

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



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

def decrement(key, amount = 1, options = {})
  increment(key, -amount, options)
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



68
69
70
71
72
73
74
75
76
77
# File 'lib/moneta/base.rb', line 68

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

Atomically increment integer value with key

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

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)


29
30
31
# File 'lib/moneta/base.rb', line 29

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