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
Constructor.
- #key?(key, options = {}) ⇒ Boolean
- #load(key, options = {}) ⇒ Object
- #store(key, value, options = {}) ⇒ Object
Methods inherited from Proxy
Methods inherited from Base
#[], #[]=, #close, #fetch, #increment
Methods included from Mixins::WithOptions
#expires, #prefix, #raw, #with
Constructor Details
#initialize(adapter, options = {}) ⇒ Expires
Constructor
Options:
-
:expires - Default expiration time (default none)
16 17 18 19 |
# File 'lib/moneta/expires.rb', line 16 def initialize(adapter, = {}) super @expires = [:expires] end |
Instance Method Details
#delete(key, options = {}) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/moneta/expires.rb', line 51 def delete(key, = {}) if .include?(:raw) super else check_expired(key, super, false) end end |
#key?(key, options = {}) ⇒ Boolean
21 22 23 |
# File 'lib/moneta/expires.rb', line 21 def key?(key, = {}) load(key, ) != nil end |
#load(key, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/moneta/expires.rb', line 25 def load(key, = {}) if .include?(:raw) super else value = check_expired(key, super) if value && .include?(:expires) store(key, value, ) else value end end end |
#store(key, value, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/moneta/expires.rb', line 38 def store(key, value, = {}) if .include?(:raw) super else if expires = (.delete(:expires) || @expires) super(key, [value, Time.now.to_i + expires], ) else super(key, [value], ) end value end end |