Class: Moneta::Expires
Overview
Adds expiration support to the underlying store
#store and #load support the :expires option to set/update the expiration time.
Instance Attribute Summary
Attributes inherited from Proxy
Instance Method Summary collapse
- #delete(key, options = {}) ⇒ Object
-
#initialize(adapter, options = {}) ⇒ Expires
constructor
A new instance of Expires.
- #key?(key, options = {}) ⇒ Boolean
- #load(key, options = {}) ⇒ Object
- #store(key, value, options = {}) ⇒ Object
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.
12 13 14 15 |
# File 'lib/moneta/expires.rb', line 12 def initialize(adapter, = {}) super @expires = [:expires] end |
Instance Method Details
#delete(key, options = {}) ⇒ Object
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.to_i <= expires end |
#key?(key, options = {}) ⇒ Boolean
17 18 19 20 21 22 23 |
# File 'lib/moneta/expires.rb', line 17 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
25 26 27 28 29 |
# File 'lib/moneta/expires.rb', line 25 def load(key, = {}) return super if .include?(:raw) value, expires = load_entry(key, ) value end |
#store(key, value, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/moneta/expires.rb', line 31 def store(key, value, = {}) return super if .include?(:raw) expires = .include?(:expires) && ( = .dup; .delete(:expires)) if expires ||= @expires super(key, [value, Time.now.to_i + expires], ) elsif Array === value || value == nil super(key, [value], ) else super(key, value, ) end value end |