Class: Moneta::Expires

Inherits:
Proxy
  • Object
show all
Includes:
ExpiresSupport
Defined in:
lib/moneta/expires.rb

Overview

Adds expiration support to the underlying store

‘#store`, `#load` and `#key?` support the `:expires` option to set/update the expiration time.

Instance Attribute Summary

Attributes inherited from Proxy

#adapter

Instance Method Summary collapse

Methods included from ExpiresSupport

included

Methods inherited from Proxy

#clear, #close, #config, #each_key, #features, features_mask, #increment, not_supports

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #close, #decrement, #each_key, #features, #fetch, included, #increment, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

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

Returns a new instance of Expires.

Parameters:

  • adapter (Moneta store)

    The underlying store

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

Options Hash (options):

  • :expires (String)

    Default expiration time



14
15
16
17
# File 'lib/moneta/expires.rb', line 14

def initialize(adapter, options = {})
  raise 'Store already supports feature :expires' if adapter.supports?(:expires)
  super
end

Instance Method Details

#create(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 Moneta::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



50
51
52
53
54
# File 'lib/moneta/expires.rb', line 50

def create(key, value, options = {})
  return super if options.include?(:raw)
  expires = expires_at(options)
  @adapter.create(key, new_entry(value, expires), Utils.without(options, :expires))
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



43
44
45
46
47
# File 'lib/moneta/expires.rb', line 43

def delete(key, options = {})
  return super if options.include?(:raw)
  value, expires = super
  value if !expires || Time.now <= Time.at(expires)
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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/moneta/expires.rb', line 74

def fetch_values(*keys, **options)
  return super if options.include?(:raw)
  new_expires = expires_at(options, nil)
  options = Utils.without(options, :expires)
  substituted = {}
  block = if block_given?
            lambda do |key|
              substituted[key] = true
              yield key
            end
          end

  with_updates(options) do |updates|
    keys.zip(@adapter.fetch_values(*keys, **options, &block)).map do |key, entry|
      next entry if substituted[key]
      entry = invalidate_entry(key, entry, new_expires) do |new_entry|
        updates[key] = new_entry
      end
      if entry == nil
        value = if block_given?
                  yield key
                end
      else
        value, = entry
      end
      value
    end
  end
end

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

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/moneta/expires.rb', line 20

def key?(key, options = {})
  # Transformer might raise exception
  load_entry(key, options) != nil
rescue
  super(key, Utils.without(options, :expires))
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 Moneta::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



28
29
30
31
32
# File 'lib/moneta/expires.rb', line 28

def load(key, options = {})
  return super if options.include?(:raw)
  value, = load_entry(key, options)
  value
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)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/moneta/expires.rb', line 123

def merge!(pairs, options = {})
  expires = expires_at(options)
  options = Utils.without(options, :expires)

  block = if block_given?
            lambda do |key, old_entry, entry|
              old_entry = invalidate_entry(key, old_entry)
              if old_entry == nil
                entry # behave as if no replace is happening
              else
                old_value, = old_entry
                new_value, = entry
                new_entry(yield(key, old_value, new_value), expires)
              end
            end
          end

  entry_pairs = pairs.map do |key, value|
    [key, new_entry(value, expires)]
  end
  @adapter.merge!(entry_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 Moneta::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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/moneta/expires.rb', line 105

def slice(*keys, **options)
  return super if options.include?(:raw)
  new_expires = expires_at(options, nil)
  options = Utils.without(options, :expires)

  with_updates(options) do |updates|
    @adapter.slice(*keys, **options).map do |key, entry|
      entry = invalidate_entry(key, entry, new_expires) do |new_entry|
        updates[key] = new_entry
      end
      next if entry == nil
      value, = entry
      [key, value]
    end.reject(&:nil?)
  end
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 Moneta::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



35
36
37
38
39
40
# File 'lib/moneta/expires.rb', line 35

def store(key, value, options = {})
  return super if options.include?(:raw)
  expires = expires_at(options)
  super(key, new_entry(value, expires), Utils.without(options, :expires))
  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.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Moneta::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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/moneta/expires.rb', line 57

def values_at(*keys, **options)
  return super if options.include?(:raw)
  new_expires = expires_at(options, nil)
  options = Utils.without(options, :expires)
  with_updates(options) do |updates|
    keys.zip(@adapter.values_at(*keys, **options)).map do |key, entry|
      entry = invalidate_entry(key, entry, new_expires) do |new_entry|
        updates[key] = new_entry
      end
      next if entry == nil
      value, = entry
      value
    end
  end
end