~ubuntu-branches/ubuntu/trusty/ruby-moneta/trusty

« back to all changes in this revision

Viewing changes to lib/moneta/expires.rb

  • Committer: Package Import Robot
  • Author(s): Jérémy Bobbio
  • Date: 2013-05-14 09:40:52 UTC
  • mfrom: (1.2.1) (3.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130514094052-p8i17yyswpx6c1zc
Tags: 0.7.16-1
* Team upload.
* New upstream version.
* Remove transitional packages.
* Add Vcs-* fields.
* Fix a typo in debian/copyright.
* Add a Breaks for chef given the API has changed between 0.6 and 0.7.
* Refresh patches:
  - remove 0002-Mark-concurrent-increment-example-as-pending.patch:
    fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Moneta
 
2
  # Adds expiration support to the underlying store
 
3
  #
 
4
  # `#store`, `#load` and `#key?` support the `:expires` option to set/update
 
5
  # the expiration time.
 
6
  #
 
7
  # @api public
 
8
  class Expires < Proxy
 
9
    include ExpiresSupport
 
10
 
 
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)
 
16
      super
 
17
      self.default_expires = options[:expires]
 
18
    end
 
19
 
 
20
    # (see Proxy#key?)
 
21
    def key?(key, options = {})
 
22
      # Transformer might raise exception
 
23
      load_entry(key, options) != nil
 
24
    rescue Exception
 
25
      super(key, Utils.without(options, :expires))
 
26
    end
 
27
 
 
28
    # (see Proxy#load)
 
29
    def load(key, options = {})
 
30
      return super if options.include?(:raw)
 
31
      value, expires = load_entry(key, options)
 
32
      value
 
33
    end
 
34
 
 
35
    # (see Proxy#store)
 
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))
 
40
      value
 
41
    end
 
42
 
 
43
    # (see Proxy#delete)
 
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
 
48
    end
 
49
 
 
50
    # (see Proxy#store)
 
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))
 
55
    end
 
56
 
 
57
    private
 
58
 
 
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)
 
63
      if entry != nil
 
64
        value, expires = entry
 
65
        if expires && Time.now.to_i > expires
 
66
          delete(key)
 
67
          nil
 
68
        elsif new_expires != nil
 
69
          @adapter.store(key, new_entry(value, new_expires), options)
 
70
          entry
 
71
        else
 
72
          entry
 
73
        end
 
74
      end
 
75
    end
 
76
 
 
77
    def new_entry(value, expires)
 
78
      if expires
 
79
        [value, expires.to_i]
 
80
      elsif Array === value || value == nil
 
81
        [value]
 
82
      else
 
83
        value
 
84
      end
 
85
    end
 
86
  end
 
87
end