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.
-
#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
#clear, #close, #features, #increment
Methods included from Defaults
#[], #[]=, #close, #decrement, #features, #fetch, included, #increment, #supports?
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.to_i <= expires end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
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 Exception 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, expires = load_entry(key, ) value end |