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
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#initialize(adapter, options = {}) ⇒ Expires
constructor
A new instance of Expires.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods inherited from Proxy
Methods included from Defaults
#[], #[]=, #close, #decrement, #fetch, #increment
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, = {}) super self.default_expires = [:expires] end |
Instance Method Details
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
48 49 50 51 52 |
# File 'lib/moneta/expires.rb', line 48 def delete(key, = {}) return super if .include?(:raw) value, expires = super value if !expires || Time.now.to_i <= expires end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
20 21 22 23 24 25 26 |
# File 'lib/moneta/expires.rb', line 20 def key?(key, = {}) # Transformer might raise exception load_entry(key, ) != nil rescue Exception .include?(:expires) && ( = .dup; .delete(:expires)) super(key, ) 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, expires = load_entry(key, ) value end |
#store(key, value, options = {}) ⇒ Object
Store value with key
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/moneta/expires.rb', line 36 def store(key, value, = {}) return super if .include?(:raw) expires = expires_at() if .include?(:expires) = .dup .delete(:expires) end store_entry(key, value, expires, ) value end |