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 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, #config, #each_key, #features, features_mask, #increment, not_supports
Methods included from Config
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 |
# File 'lib/moneta/expires.rb', line 14 def initialize(adapter, = {}) raise 'Store already supports feature :expires' if adapter.supports?(:expires) super end |
Instance Method Details
#create(key, value, options = {}) ⇒ Object
Store value with key
50 51 52 53 54 |
# File 'lib/moneta/expires.rb', line 50 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
43 44 45 46 47 |
# File 'lib/moneta/expires.rb', line 43 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.
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, **) 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
20 21 22 23 24 25 |
# File 'lib/moneta/expires.rb', line 20 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
28 29 30 31 32 |
# File 'lib/moneta/expires.rb', line 28 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.
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, = {}) 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.
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, **) 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
35 36 37 38 39 40 |
# File 'lib/moneta/expires.rb', line 35 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.
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, **) 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 |