Module: Moneta::HashAdapter Private

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


468
469
470
# File 'lib/moneta/mixins.rb', line 468

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store


491
492
493
494
# File 'lib/moneta/mixins.rb', line 491

def clear(options = {})
  @backend.clear
  self
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware


486
487
488
# File 'lib/moneta/mixins.rb', line 486

def delete(key, options = {})
  @backend.delete(key)
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


503
504
505
506
507
# File 'lib/moneta/mixins.rb', line 503

def fetch_values(*keys, **options, &defaults)
  return super unless @backend.respond_to? :fetch_values
  defaults ||= {} # prevents KeyError
  @backend.fetch_values(*keys, &defaults)
end

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

Exists the value with key

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


471
472
473
# File 'lib/moneta/mixins.rb', line 471

def key?(key, options = {})
  @backend.has_key?(key)
end

#load(key, options = {}) ⇒ Object

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

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


476
477
478
# File 'lib/moneta/mixins.rb', line 476

def load(key, options = {})
  @backend[key]
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 = {}) {|key, old_value, new_value| ... } ⇒ self

    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


516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/moneta/mixins.rb', line 516

def merge!(pairs, options={}, &block)
  return super unless method = [:merge!, :update].find do |method|
    @backend.respond_to? method
  end

  hash = Hash === pairs ? pairs : Hash[pairs.to_a]
  case method
  when :merge!
    @backend.merge!(hash, &block)
  when :update
    @backend.update(hash, &block)
  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.

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


510
511
512
513
# File 'lib/moneta/mixins.rb', line 510

def slice(*keys, **options)
  return super unless @backend.respond_to? :slice
  @backend.slice(*keys)
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware


481
482
483
# File 'lib/moneta/mixins.rb', line 481

def store(key, value, options = {})
  @backend[key] = value
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.

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


497
498
499
500
# File 'lib/moneta/mixins.rb', line 497

def values_at(*keys, **options)
  return super unless @backend.respond_to? :values_at
  @backend.values_at(*keys)
end