2
# Adds expiration support to the underlying store
4
# `#store`, `#load` and `#key?` support the `:expires` option to set/update
11
# @param [Moneta store] adapter The underlying store
12
# @param [Hash] options
13
# @option options [String] :expires Default expiration time
14
def initialize(adapter, options = {})
15
raise 'Store already supports feature :expires' if adapter.supports?(:expires)
17
self.default_expires = options[:expires]
21
def key?(key, options = {})
22
# Transformer might raise exception
23
load_entry(key, options) != nil
25
super(key, Utils.without(options, :expires))
29
def load(key, options = {})
30
return super if options.include?(:raw)
31
value, expires = load_entry(key, options)
36
def store(key, value, options = {})
37
return super if options.include?(:raw)
38
expires = expires_at(options)
39
super(key, new_entry(value, expires), Utils.without(options, :expires))
44
def delete(key, options = {})
45
return super if options.include?(:raw)
46
value, expires = super
47
value if !expires || Time.now.to_i <= expires
51
def create(key, value, options = {})
52
return super if options.include?(:raw)
53
expires = expires_at(options)
54
@adapter.create(key, new_entry(value, expires), Utils.without(options, :expires))
59
def load_entry(key, options)
60
new_expires = expires_at(options, nil)
61
options = Utils.without(options, :expires)
62
entry = @adapter.load(key, options)
64
value, expires = entry
65
if expires && Time.now.to_i > expires
68
elsif new_expires != nil
69
@adapter.store(key, new_entry(value, new_expires), options)
77
def new_entry(value, expires)
80
elsif Array === value || value == nil