diff options
author | Luke Kanies <luke@madstop.com> | 2008-05-13 16:00:58 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-05-13 16:00:58 -0500 |
commit | 6efe4000dda3379e867786a9c2d4ae0f0cdfc3be (patch) | |
tree | a16fd1ae1f4aab7fe04af88daa78f1be1a2b1f3e /lib/puppet | |
parent | 68d8d0ae0686939d94dae8ccc70e5582187335dc (diff) | |
download | puppet-6efe4000dda3379e867786a9c2d4ae0f0cdfc3be.tar.gz puppet-6efe4000dda3379e867786a9c2d4ae0f0cdfc3be.tar.xz puppet-6efe4000dda3379e867786a9c2d4ae0f0cdfc3be.zip |
Using the new Cacher class for handling cached data.
This provides a single, global bit for determining whether
a given piece of cached data is still valid.
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/file_serving/configuration.rb | 13 | ||||
-rw-r--r-- | lib/puppet/file_serving/mount.rb | 25 | ||||
-rw-r--r-- | lib/puppet/indirector/indirection.rb | 22 | ||||
-rw-r--r-- | lib/puppet/network/http_pool.rb | 32 | ||||
-rw-r--r-- | lib/puppet/ssl/certificate_authority.rb | 9 | ||||
-rw-r--r-- | lib/puppet/util/cacher.rb | 18 |
6 files changed, 54 insertions, 65 deletions
diff --git a/lib/puppet/file_serving/configuration.rb b/lib/puppet/file_serving/configuration.rb index ccf0957d1..9c38aaa19 100644 --- a/lib/puppet/file_serving/configuration.rb +++ b/lib/puppet/file_serving/configuration.rb @@ -5,25 +5,20 @@ require 'puppet' require 'puppet/file_serving' require 'puppet/file_serving/mount' +require 'puppet/util/cacher' class Puppet::FileServing::Configuration require 'puppet/file_serving/configuration/parser' + extend Puppet::Util::Cacher + @config_fileuration = nil Mount = Puppet::FileServing::Mount - # Remove our singleton instance. - def self.clear_cache - @config_fileuration = nil - end - # Create our singleton configuration. def self.create - unless @config_fileuration - @config_fileuration = new() - end - @config_fileuration + attr_cache(:configuration) { new() } end private_class_method :new diff --git a/lib/puppet/file_serving/mount.rb b/lib/puppet/file_serving/mount.rb index 8e5bd03e8..c52cedbfb 100644 --- a/lib/puppet/file_serving/mount.rb +++ b/lib/puppet/file_serving/mount.rb @@ -4,6 +4,7 @@ require 'puppet/network/authstore' require 'puppet/util/logging' +require 'puppet/util/cacher' require 'puppet/file_serving' require 'puppet/file_serving/metadata' require 'puppet/file_serving/content' @@ -12,12 +13,16 @@ require 'puppet/file_serving/content' # or content objects. class Puppet::FileServing::Mount < Puppet::Network::AuthStore include Puppet::Util::Logging + extend Puppet::Util::Cacher - @@localmap = nil - - # Clear the cache. This is only ever used for testing. - def self.clear_cache - @@localmap = nil + def self.localmap + attr_cache(:localmap) { + { "h" => Facter.value("hostname"), + "H" => [Facter.value("hostname"), + Facter.value("domain")].join("."), + "d" => Facter.value("domain") + } + } end attr_reader :name @@ -173,14 +178,6 @@ class Puppet::FileServing::Mount < Puppet::Network::AuthStore # Cache this manufactured map, since if it's used it's likely # to get used a lot. def localmap - unless @@localmap - @@localmap = { - "h" => Facter.value("hostname"), - "H" => [Facter.value("hostname"), - Facter.value("domain")].join("."), - "d" => Facter.value("domain") - } - end - @@localmap + self.class.localmap end end diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 05464f8c9..c70259304 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -1,20 +1,17 @@ require 'puppet/util/docs' require 'puppet/indirector/envelope' require 'puppet/indirector/request' +require 'puppet/util/cacher' # The class that connects functional classes with their different collection # back-ends. Each indirection has a set of associated terminus classes, # each of which is a subclass of Puppet::Indirector::Terminus. class Puppet::Indirector::Indirection + include Puppet::Util::Cacher include Puppet::Util::Docs @@indirections = [] - # Clear all cached termini from all indirections. - def self.clear_cache - @@indirections.each { |ind| ind.clear_cache } - end - # Find an indirection by name. This is provided so that Terminus classes # can specifically hook up with the indirections they are associated with. def self.instance(name) @@ -54,13 +51,6 @@ class Puppet::Indirector::Indirection @cache_class = class_name end - # Clear our cached list of termini, and reset the cache name - # so it's looked up again. - # This is only used for testing. - def clear_cache - @termini.clear - end - # This is only used for testing. def delete @@indirections.delete(self) if @@indirections.include?(self) @@ -104,7 +94,6 @@ class Puppet::Indirector::Indirection @model = model @name = name - @termini = {} @cache_class = nil @terminus_class = nil @@ -138,7 +127,7 @@ class Puppet::Indirector::Indirection raise Puppet::DevError, "No terminus specified for %s; cannot redirect" % self.name end - return @termini[terminus_name] ||= make_terminus(terminus_name) + return termini[terminus_name] ||= make_terminus(terminus_name) end # This can be used to select the terminus class. @@ -299,4 +288,9 @@ class Puppet::Indirector::Indirection end return klass.new end + + # Use cached termini. + def termini + attr_cache(:termini) { Hash.new } + end end diff --git a/lib/puppet/network/http_pool.rb b/lib/puppet/network/http_pool.rb index cf17a8380..78a35cc15 100644 --- a/lib/puppet/network/http_pool.rb +++ b/lib/puppet/network/http_pool.rb @@ -1,10 +1,13 @@ require 'puppet/ssl/host' require 'net/https' +require 'puppet/util/cacher' module Puppet::Network; end # Manage Net::HTTP instances for keep-alive. module Puppet::Network::HttpPool + extend Puppet::Util::Cacher + # 2008/03/23 # LAK:WARNING: Enabling this has a high propability of # causing corrupt files and who knows what else. See #1010. @@ -17,23 +20,15 @@ module Puppet::Network::HttpPool # Create an ssl host instance for getting certificate # information. def self.ssl_host - unless defined?(@ssl_host) and @ssl_host - @ssl_host = Puppet::SSL::Host.new - end - @ssl_host + attr_cache(:ssl_host) { Puppet::SSL::Host.new } end - @http_cache = {} - # Clear our http cache, closing all connections. def self.clear_http_instances - @http_cache.each do |name, connection| + http_cache.each do |name, connection| connection.finish if connection.started? end - @http_cache.clear - @cert = nil - @key = nil - @ssl_host = nil + Puppet::Util::Cacher.invalidate end # Make sure we set the driver up when we read the cert in. @@ -69,11 +64,11 @@ module Puppet::Network::HttpPool # Return our cached instance if we've got a cache, as long as we're not # resetting the instance. if keep_alive? - return @http_cache[key] if ! reset and @http_cache[key] + return http_cache[key] if ! reset and http_cache[key] # Clean up old connections if we have them. - if http = @http_cache[key] - @http_cache.delete(key) + if http = http_cache[key] + http_cache.delete(key) http.finish if http.started? end end @@ -103,8 +98,15 @@ module Puppet::Network::HttpPool cert_setup(http) - @http_cache[key] = http if keep_alive? + http_cache[key] = http if keep_alive? return http end + + private + + def self.http_cache + # Default to an empty hash. + attr_cache(:http) { Hash.new } + end end diff --git a/lib/puppet/ssl/certificate_authority.rb b/lib/puppet/ssl/certificate_authority.rb index cd5d79f0a..6947af11c 100644 --- a/lib/puppet/ssl/certificate_authority.rb +++ b/lib/puppet/ssl/certificate_authority.rb @@ -1,5 +1,6 @@ require 'puppet/ssl/host' require 'puppet/ssl/certificate_request' +require 'puppet/util/cacher' # The class that knows how to sign certificates. It creates # a 'special' SSL::Host whose name is 'ca', thus indicating @@ -16,6 +17,8 @@ class Puppet::SSL::CertificateAuthority require 'puppet/ssl/certificate_authority/interface' + extend Puppet::Util::Cacher + def self.ca? return false unless Puppet[:ca] return false unless Puppet[:name] == "puppetmasterd" @@ -27,11 +30,7 @@ class Puppet::SSL::CertificateAuthority def self.instance return nil unless ca? - unless defined?(@instance) and @instance - @instance = new - end - - @instance + attr_cache(:instance) { new } end attr_reader :name, :host diff --git a/lib/puppet/util/cacher.rb b/lib/puppet/util/cacher.rb index fbab9ab42..7b352c72a 100644 --- a/lib/puppet/util/cacher.rb +++ b/lib/puppet/util/cacher.rb @@ -26,7 +26,7 @@ module Puppet::Util::Cacher def cached_attr(name, &block) define_method(name) do - cache(name, &block) + attr_cache(name, &block) end end end @@ -34,7 +34,7 @@ module Puppet::Util::Cacher module InstanceMethods private - def cache(name, &block) + def attr_cache(name, &block) unless defined?(@cacher_caches) and @cacher_caches @cacher_caches = Cache.new end @@ -44,24 +44,26 @@ module Puppet::Util::Cacher end class Cache - attr_reader :timestamp, :caches + attr_accessor :caches, :timestamp 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) + 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 + unless caches.include?(name) + caches[name] = yield end - @caches[name] + caches[name] end end end |