Module: Moneta::Defaults

Overview

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#[](key) ⇒ Object

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

Parameters:

  • key (Object)

Returns:

  • (Object)

    value



177
178
179
# File 'lib/moneta/mixins.rb', line 177

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



187
188
189
# File 'lib/moneta/mixins.rb', line 187

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

#closeObject

Explicitly close the store

Returns:

  • nil



135
136
# File 'lib/moneta/mixins.rb', line 135

def 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

Raises:

  • (NotImplementedError)


203
204
205
# File 'lib/moneta/mixins.rb', line 203

def create(key, value, options = {})
  raise NotImplementedError, 'create is not supported'
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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



128
129
130
# File 'lib/moneta/mixins.rb', line 128

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

#featuresArray<Symbol>

Returns features list

Returns:

  • (Array<Symbol>)

    list of features



210
211
212
# File 'lib/moneta/mixins.rb', line 210

def features
  self.class.features
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: {})

    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:

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

    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:

    • (Object)

      value from store



161
162
163
164
165
166
167
168
169
170
# File 'lib/moneta/mixins.rb', line 161

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

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/moneta/mixins.rb', line 111

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 Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


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

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

#supports?(feature) ⇒ Boolean

Return true if adapter supports the given feature.

Returns:

  • (Boolean)


217
218
219
# File 'lib/moneta/mixins.rb', line 217

def supports?(feature)
  features.include?(feature)
end