Module: Moneta::Defaults

Includes:
OptionSupport
Included in:
Adapter, Adapters::DataMapper, Adapters::File, Adapters::Null, Cache, Proxy, Stack
Defined in:
lib/moneta/defaults.rb

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



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

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



145
146
147
# File 'lib/moneta/defaults.rb', line 145

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • value



155
156
157
# File 'lib/moneta/defaults.rb', line 155

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

#closeObject

Explicitly close the store

Returns:

  • nil



104
# File 'lib/moneta/defaults.rb', line 104

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)


189
190
191
# File 'lib/moneta/defaults.rb', line 189

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



97
98
99
# File 'lib/moneta/defaults.rb', line 97

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

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

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

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)

Raises:

  • (NotImplementedError)


173
174
175
# File 'lib/moneta/defaults.rb', line 173

def each_key
  raise NotImplementedError, 'each_key is not supported'
end

#featuresArray<Symbol>

Returns features list

Returns:

  • (Array<Symbol>)

    list of features



309
310
311
# File 'lib/moneta/defaults.rb', line 309

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/moneta/defaults.rb', line 129

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

#fetch_values(*keys, **options) ⇒ Object #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Behaves identically to #values_at except that it accepts an optional block. When supplied, the block will be called successively with each supplied key that is not present in the store. The return value of the block call will be used in place of nil in returned the array of values.

Overloads:

  • #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

    Returns Array containing the values requested, or where keys are missing, the return values from the corresponding block calls.

    Yield Parameters:

    • key (Object)

      Each key that is not found in the store

    Yield Returns:

    • (Object, nil)

      The value to substitute for the missing one

    Returns:

    • (Array<Object, nil>)

      Array containing the values requested, or where keys are missing, the return values from the corresponding block calls



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/moneta/defaults.rb', line 231

def fetch_values(*keys, **options)
  values = values_at(*keys, **options)
  return values unless block_given?
  keys.zip(values).map do |key, value|
    if value == nil
      yield key
    else
      value
    end
  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)


80
81
82
# File 'lib/moneta/defaults.rb', line 80

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)


63
64
65
# File 'lib/moneta/defaults.rb', line 63

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

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


290
291
292
293
294
295
296
297
298
299
# File 'lib/moneta/defaults.rb', line 290

def merge!(pairs, options = {})
  pairs.each do |key, value|
    if block_given?
      existing = load(key, options)
      value = yield(key, existing, value) unless existing == nil
    end
    store(key, value, options)
  end
  self
end

#slice(*keys, **options) ⇒ <(Object, Object)>

Note:

The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

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, Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (<(Object, Object)>)

    A collection of key-value pairs



260
261
262
263
264
# File 'lib/moneta/defaults.rb', line 260

def slice(*keys, **options)
  keys.zip(values_at(*keys, **options)).reject do |_, value|
    value == nil
  end
end

#supports?(feature) ⇒ Boolean

Return true if adapter supports the given feature.

Returns:

  • (Boolean)


316
317
318
# File 'lib/moneta/defaults.rb', line 316

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

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


302
303
304
# File 'lib/moneta/defaults.rb', line 302

def update(pairs, options = {}, &block)
  merge!(pairs, options, &block)
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

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, Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values



206
207
208
# File 'lib/moneta/defaults.rb', line 206

def values_at(*keys, **options)
  keys.map { |key| load(key, options) }
end