summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/cacher.rb
blob: 7b352c72a6a6b1897c3467865bbbf2c7ddd69bf6 (plain)
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
module Puppet::Util::Cacher
    # Cause all cached values to be considered invalid.
    def self.invalidate
        @timestamp = Time.now
    end

    def self.valid?(timestamp)
        unless defined?(@timestamp) and @timestamp
            @timestamp = Time.now
            return true
        end
        return timestamp >= @timestamp
    end

    def self.extended(other)
        other.extend(InstanceMethods)
    end

    def self.included(other)
        other.extend(ClassMethods)
        other.send(:include, InstanceMethods)
    end

    module ClassMethods
        private

        def cached_attr(name, &block)
            define_method(name) do
                attr_cache(name, &block)
            end
        end
    end

    module InstanceMethods
        private

        def attr_cache(name, &block)
            unless defined?(@cacher_caches) and @cacher_caches
                @cacher_caches = Cache.new
            end

            @cacher_caches.value(name, &block)
        end
    end

    class Cache
        attr_accessor :caches, :timestamp

        def initialize
            @caches = {}
        end

        def value(name)
            raise ArgumentError, "You must provide a block when using the cache" unless block_given?

            if timestamp.nil? or ! Puppet::Util::Cacher.valid?(timestamp)
                caches.clear
                self.timestamp = Time.now
            end

            # Use 'include?' here rather than testing for truth, so we
            # can cache false values.
            unless caches.include?(name)
                caches[name] = yield
            end
            caches[name]
        end
    end
end