From 68d8d0ae0686939d94dae8ccc70e5582187335dc Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 7 May 2008 18:28:35 -0500 Subject: Adding a module for handling caching information. I keep having issues with integration tests keeping cached values around, and this module should hopefully give us a single place to invalidate all caches, thus making testing this much easier. --- lib/puppet/util/cacher.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lib/puppet/util/cacher.rb (limited to 'lib/puppet') diff --git a/lib/puppet/util/cacher.rb b/lib/puppet/util/cacher.rb new file mode 100644 index 000000000..fbab9ab42 --- /dev/null +++ b/lib/puppet/util/cacher.rb @@ -0,0 +1,67 @@ +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 + cache(name, &block) + end + end + end + + module InstanceMethods + private + + def 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_reader :timestamp, :caches + + def initialize + @timestamp = Time.now + @caches = {} + end + + def value(name) + raise ArgumentError, "You must provide a block when using the cache" unless block_given? + + @caches.clear unless Puppet::Util::Cacher.valid?(@timestamp) + + # 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 -- cgit