Class: Moneta::Expires

Inherits:
Proxy show all
Defined in:
lib/moneta/expires.rb

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

#adapter

Instance Method Summary collapse

Methods inherited from Proxy

#clear, #close, #increment

Methods inherited from Base

#[], #[]=, #close, #decrement, #fetch, #increment

Methods included from Mixins::WithOptions

#expires, #prefix, #raw, #with

Constructor Details

#initialize(adapter, options = {}) ⇒ Expires

Constructor

Parameters:

  • adapter (Moneta store)

    The underlying store

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (String)

    Default expiration time



14
15
16
17
# File 'lib/moneta/expires.rb', line 14

def initialize(adapter, options = {})
  super
  @expires = options[:expires]
end

Instance Method Details

#delete(key, options = {}) ⇒ Object



39
40
41
42
43
# File 'lib/moneta/expires.rb', line 39

def delete(key, options = {})
  return super if options.include?(:raw)
  value, expires = super
  value if !expires || Time.now.to_i <= expires
end

#key?(key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/moneta/expires.rb', line 19

def key?(key, options = {})
  load_entry(key, options) != nil
end

#load(key, options = {}) ⇒ Object



23
24
25
26
27
# File 'lib/moneta/expires.rb', line 23

def load(key, options = {})
  return super if options.include?(:raw)
  value, expires = load_entry(key, options)
  value
end

#store(key, value, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/moneta/expires.rb', line 29

def store(key, value, options = {})
  return super if options.include?(:raw)
  if expires = (options.delete(:expires) || @expires)
    super(key, [value, Time.now.to_i + expires], options)
  else
    super(key, [value], options)
  end
  value
end