Class: Moneta::Expires
- 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 included from ExpiresSupport
Attributes inherited from Proxy
Instance Method Summary collapse
-
#create(key, value, options = {}) ⇒ Object
Store value with key.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#fetch_values(*keys, **options) ⇒ Object
Behaves identically to #values_at except that it accepts an optional block.
-
#initialize(adapter, options = {}) ⇒ Expires
constructor
A new instance of Expires.
- #key?(key, options = {}) ⇒ Boolean
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from ExpiresSupport
Methods inherited from Proxy
#clear, #close, #each_key, #features, features_mask, #increment, not_supports
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.
14 15 16 17 18 |
# File 'lib/moneta/expires.rb', line 14 def initialize(adapter, = {}) raise 'Store already supports feature :expires' if adapter.supports?(:expires) super self.default_expires = [:expires] end |
Instance Method Details
#create(key, value, options = {}) ⇒ Object
Store value with key
51 52 53 54 55 |
# File 'lib/moneta/expires.rb', line 51 def create(key, value, = {}) return super if .include?(:raw) expires = expires_at() @adapter.create(key, new_entry(value, expires), Utils.without(, :expires)) end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
44 45 46 47 48 |
# File 'lib/moneta/expires.rb', line 44 def delete(key, = {}) return super if .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>
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.
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 103 |
# File 'lib/moneta/expires.rb', line 75 def fetch_values(*keys, **) return super if .include?(:raw) new_expires = expires_at(, nil) = Utils.without(, :expires) substituted = {} block = if block_given? lambda do |key| substituted[key] = true yield key end end with_updates() do |updates| keys.zip(@adapter.fetch_values(*keys, **, &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
21 22 23 24 25 26 |
# File 'lib/moneta/expires.rb', line 21 def key?(key, = {}) # Transformer might raise exception load_entry(key, ) != nil rescue super(key, Utils.without(, :expires)) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
29 30 31 32 33 |
# File 'lib/moneta/expires.rb', line 29 def load(key, = {}) return super if .include?(:raw) value, = load_entry(key, ) value end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/moneta/expires.rb', line 124 def merge!(pairs, = {}) expires = expires_at() = Utils.without(, :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, , &block) self end |
#slice(*keys, **options) ⇒ <(Object, Object)>
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#==).
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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/moneta/expires.rb', line 106 def slice(*keys, **) return super if .include?(:raw) new_expires = expires_at(, nil) = Utils.without(, :expires) with_updates() do |updates| @adapter.slice(*keys, **).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
36 37 38 39 40 41 |
# File 'lib/moneta/expires.rb', line 36 def store(key, value, = {}) return super if .include?(:raw) expires = expires_at() super(key, new_entry(value, expires), Utils.without(, :expires)) value end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/moneta/expires.rb', line 58 def values_at(*keys, **) return super if .include?(:raw) new_expires = expires_at(, nil) = Utils.without(, :expires) with_updates() do |updates| keys.zip(@adapter.values_at(*keys, **)).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 |