Class: Moneta::Proxy

Inherits:
Object
  • Object
show all
Includes:
Config, Defaults
Defined in:
lib/moneta/proxy.rb

Overview

Proxy base class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

included

Methods included from Defaults

#[], #[]=, #decrement, #fetch, included, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(adapter, options = {}) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • adapter (Moneta store)

    underlying adapter

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


12
13
14
15
# File 'lib/moneta/proxy.rb', line 12

def initialize(adapter, options = {})
  @adapter = adapter
  configure(**options)
end

Instance Attribute Details

#adapterObject (readonly)



8
9
10
# File 'lib/moneta/proxy.rb', line 8

def adapter
  @adapter
end

Class Method Details

.features_maskObject

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.



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

def features_mask
  @features_mask ||= [].freeze
end

.not_supports(*features) ⇒ Object

Declares that this adapter does not support the given feature, and adds a stub method that raises a NotImplementedError. Useful when inheriting from another adapter.

Examples:

class MyAdapter < OtherAdapterWithCreate
  include Moneta::Defaults
  not_supports :create
end


133
134
135
136
# File 'lib/moneta/proxy.rb', line 133

def not_supports(*features)
  @features_mask = (features_mask | features).freeze
  super
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


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

def clear(options = {})
  adapter.clear(options)
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



43
44
45
# File 'lib/moneta/proxy.rb', line 43

def close
  adapter.close
end

#configObject

Overrides the default implementation of the config method to:

  • pass the adapter’s config, if this proxy has no configuration of its own

  • return a merged configuration, allowing the proxy have precedence over the adapter



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/moneta/proxy.rb', line 145

def config
  unless @proxy_config
    config = super
    adapter_config = adapter.config if adapter.class.include?(Config)

    @proxy_config =
      if config && adapter_config
        adapter_members = adapter_config.members - config.members
        members = config.members + adapter_members
        struct = Struct.new(*members)

        values = config.values + adapter_config.to_h.values_at(*adapter_members)
        struct.new(*values)
      else
        config || adapter_config
      end
  end

  @proxy_config
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



38
39
40
# File 'lib/moneta/proxy.rb', line 38

def create(key, value, options = {})
  adapter.create(key, value, options)
end

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

Delete the key from the store and return the current value

Parameters:

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

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

Returns:

  • (Object)

    current value



86
87
88
# File 'lib/moneta/proxy.rb', line 86

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


23
24
25
26
27
28
29
30
# File 'lib/moneta/proxy.rb', line 23

def each_key(&block)
  raise NotImplementedError, "each_key is not supported on this proxy" \
    unless supports? :each_key

  return enum_for(:each_key) { adapter.each_key.size } unless block_given?
  adapter.each_key(&block)
  self
end

#featuresArray<Symbol>

Returns features list

Returns:

  • (Array<Symbol>)

    list of features



122
123
124
# File 'lib/moneta/proxy.rb', line 122

def features
  @features ||= (self.class.features | adapter.features - self.class.features_mask).freeze
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



106
107
108
# File 'lib/moneta/proxy.rb', line 106

def fetch_values(*keys, **options, &defaults)
  adapter.fetch_values(*keys, **options, &defaults)
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



33
34
35
# File 'lib/moneta/proxy.rb', line 33

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


18
19
20
# File 'lib/moneta/proxy.rb', line 18

def key?(key, options = {})
  adapter.key?(key, options)
end

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

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

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)

  • :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)

    value



58
59
60
# File 'lib/moneta/proxy.rb', line 58

def load(key, options = {})
  adapter.load(key, options)
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)


116
117
118
119
# File 'lib/moneta/proxy.rb', line 116

def merge!(pairs, options = {}, &block)
  adapter.merge!(pairs, options, &block)
  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



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

def slice(*keys, **options)
  adapter.slice(*keys, **options)
end

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

Store value with key

Parameters:

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

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

Returns:

  • value



73
74
75
# File 'lib/moneta/proxy.rb', line 73

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



101
102
103
# File 'lib/moneta/proxy.rb', line 101

def values_at(*keys, **options)
  adapter.values_at(*keys, **options)
end