/usr/lib/ruby/vendor_ruby/moneta/expires.rb is in ruby-moneta 0.7.20-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | module Moneta
# Adds expiration support to the underlying store
#
# `#store`, `#load` and `#key?` support the `:expires` option to set/update
# the expiration time.
#
# @api public
class Expires < Proxy
include ExpiresSupport
# @param [Moneta store] adapter The underlying store
# @param [Hash] options
# @option options [String] :expires Default expiration time
def initialize(adapter, options = {})
raise 'Store already supports feature :expires' if adapter.supports?(:expires)
super
self.default_expires = options[:expires]
end
# (see Proxy#key?)
def key?(key, options = {})
# Transformer might raise exception
load_entry(key, options) != nil
rescue Exception
super(key, Utils.without(options, :expires))
end
# (see Proxy#load)
def load(key, options = {})
return super if options.include?(:raw)
value, expires = load_entry(key, options)
value
end
# (see Proxy#store)
def store(key, value, options = {})
return super if options.include?(:raw)
expires = expires_at(options)
super(key, new_entry(value, expires), Utils.without(options, :expires))
value
end
# (see Proxy#delete)
def delete(key, options = {})
return super if options.include?(:raw)
value, expires = super
value if !expires || Time.now.to_i <= expires
end
# (see Proxy#store)
def create(key, value, options = {})
return super if options.include?(:raw)
expires = expires_at(options)
@adapter.create(key, new_entry(value, expires), Utils.without(options, :expires))
end
private
def load_entry(key, options)
new_expires = expires_at(options, nil)
options = Utils.without(options, :expires)
entry = @adapter.load(key, options)
if entry != nil
value, expires = entry
if expires && Time.now.to_i > expires
delete(key)
nil
elsif new_expires != nil
@adapter.store(key, new_entry(value, new_expires), options)
entry
else
entry
end
end
end
def new_entry(value, expires)
if expires
[value, expires.to_i]
elsif Array === value || value == nil
[value]
else
value
end
end
end
end
|