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, #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)

Parameters:

  • adapter (Moneta store)

    The underlying store

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


16
17
18
19
# File 'lib/moneta/expires.rb', line 16

def initialize(adapter, options = {})
  super
  @expires = options[: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, options = {})
  if options.include?(:raw)
    super
  else
    check_expired(key, super, false)
  end
end

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

Returns:

  • (Boolean)


21
22
23
# File 'lib/moneta/expires.rb', line 21

def key?(key, options = {})
  load(key, options) != 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, options = {})
  if options.include?(:raw)
    super
  else
    value = check_expired(key, super)
    if value && options.include?(:expires)
      store(key, value, options)
    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, options = {})
  if options.include?(:raw)
    super
  else
    if expires = (options.delete(:expires) || @expires)
      super(key, [value, Time.now.to_i + expires], options)
    else
      super(key, [value], options)
    end
    value
  end
end