blob: 136c9973e79b021b644871e24962068f7c6161eb (
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
70
71
72
73
74
75
76
77
78
79
80
|
require 'monitor'
module Puppet::Util::Cacher
# Our module has been extended in a class; we can only add the Instance methods,
# which become *class* methods in the class.
def self.extended(other)
class << other
extend ClassMethods
include InstanceMethods
end
end
# Our module has been included in a class, which means the class gets the class methods
# and all of its instances get the instance methods.
def self.included(other)
other.extend(ClassMethods)
other.send(:include, InstanceMethods)
end
# Methods that can get added to a class.
module ClassMethods
# Provide a means of defining an attribute whose value will be cached.
# Must provide a block capable of defining the value if it's flushed..
def cached_attr(name, ttl, &block)
init_method = "init_#{name}"
define_method(init_method, &block)
set_attr_ttl(name, ttl)
define_method(name) do
cached_value(name)
end
define_method(name.to_s + "=") do |value|
# Make sure the cache timestamp is set
value_cache.synchronize do
value_cache[name] = value
set_expiration(name)
end
end
end
def attr_ttl(name)
@attr_ttls[name]
end
def set_attr_ttl(name, value)
@attr_ttls ||= {}
@attr_ttls[name] = Integer(value)
end
end
# Methods that get added to instances.
module InstanceMethods
private
def cached_value(name)
value_cache.synchronize do
if value_cache[name].nil? or expired_by_ttl?(name)
value_cache[name] = send("init_#{name}")
set_expiration(name)
end
value_cache[name]
end
end
def expired_by_ttl?(name)
@attr_expirations[name] < Time.now
end
def set_expiration(name)
@attr_expirations ||= {}
@attr_expirations[name] = Time.now + self.class.attr_ttl(name)
end
def value_cache
@value_cache ||= {}.extend(MonitorMixin)
end
end
end
|