Class: Moneta::Expires

Inherits:
Proxy
  • Object
show all
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

#default_expires

Attributes inherited from Proxy

#adapter

Instance Method Summary collapse

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.

Parameters:

  • adapter (Moneta store)

    The underlying store

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

Options Hash (options):

  • :expires (String)

    Default expiration time



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

def initialize(adapter, options = {})
  raise 'Store already supports feature :expires' if adapter.supports?(:expires)
  super
  self.default_expires = options[:expires]
end

Instance Method Details

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

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Moneta::Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



51
52
53
54
55
# File 'lib/moneta/expires.rb', line 51

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

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

Delete the key from the store and return the current value

Parameters:

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

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



44
45
46
47
48
# File 'lib/moneta/expires.rb', line 44

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

Exists the value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Moneta::Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


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

def key?(key, options = {})
  # Transformer might raise exception
  load_entry(key, options) != nil
rescue Exception
  super(key, Utils.without(options, :expires))
end

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

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Moneta::Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Adapters::Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



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

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

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

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Moneta::Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



36
37
38
39
40
41
# File 'lib/moneta/expires.rb', line 36

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