summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-05-17 16:54:06 -0500
committerJames Turnbull <james@lovedthanlost.net>2009-05-20 18:29:04 +1000
commit5f1c228767432591a7e4b77666e9b1c7d70a095c (patch)
tree17d23e81ce3918790e78d13c2dbdd485124bfa87
parent047ab782aed5555a6812131acdba3925b1274a55 (diff)
downloadpuppet-5f1c228767432591a7e4b77666e9b1c7d70a095c.tar.gz
puppet-5f1c228767432591a7e4b77666e9b1c7d70a095c.tar.xz
puppet-5f1c228767432591a7e4b77666e9b1c7d70a095c.zip
Adding caching to the Environment class
Caching the module path (because we check which directories exist, and this method can get called often), and the complete list of modules. The cache ttl uses the filetimeout, which defaults to 15 seconds. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/node/environment.rb20
-rwxr-xr-xspec/unit/node/environment.rb16
2 files changed, 34 insertions, 2 deletions
diff --git a/lib/puppet/node/environment.rb b/lib/puppet/node/environment.rb
index 3d13af1f8..c053e0d5f 100644
--- a/lib/puppet/node/environment.rb
+++ b/lib/puppet/node/environment.rb
@@ -1,6 +1,14 @@
+require 'puppet/util/cacher'
+
+# Just define it, so this class has fewer load dependencies.
+class Puppet::Node
+end
+
# Model the environment that a node can operate in. This class just
# provides a simple wrapper for the functionality around environments.
class Puppet::Node::Environment
+ include Puppet::Util::Cacher
+
@seen = {}
# Return an existing environment instance, or create a new one.
@@ -18,6 +26,11 @@ class Puppet::Node::Environment
@seen[symbol] = obj
end
+ # This is only used for testing.
+ def self.clear
+ @seen.clear
+ end
+
attr_reader :name
# Return an environment-specific setting.
@@ -35,7 +48,9 @@ class Puppet::Node::Environment
return mod
end
- def modulepath
+ # Cache the modulepath, so that we aren't searching through
+ # all known directories all the time.
+ cached_attr(:modulepath, :ttl => Puppet[:filetimeout]) do
dirs = self[:modulepath].split(File::PATH_SEPARATOR)
if ENV["PUPPETLIB"]
dirs = ENV["PUPPETLIB"].split(File::PATH_SEPARATOR) + dirs
@@ -52,7 +67,8 @@ class Puppet::Node::Environment
end
# Return all modules from this environment.
- def modules
+ # Cache the list, because it can be expensive to create.
+ cached_attr(:modules, :ttl => Puppet[:filetimeout]) do
result = []
Puppet::Module.each_module(modulepath) do |mod|
result << mod
diff --git a/spec/unit/node/environment.rb b/spec/unit/node/environment.rb
index 5fac98b77..3b58a115b 100755
--- a/spec/unit/node/environment.rb
+++ b/spec/unit/node/environment.rb
@@ -6,6 +6,22 @@ require 'puppet/node/environment'
require 'puppet/util/execution'
describe Puppet::Node::Environment do
+ after do
+ Puppet::Node::Environment.clear
+ end
+
+ it "should include the Cacher module" do
+ Puppet::Node::Environment.ancestors.should be_include(Puppet::Util::Cacher)
+ end
+
+ it "should use the filetimeout for the ttl for the modulepath" do
+ Puppet::Node::Environment.attr_ttl(:modulepath).should == Integer(Puppet[:filetimeout])
+ end
+
+ it "should use the filetimeout for the ttl for the module list" do
+ Puppet::Node::Environment.attr_ttl(:modules).should == Integer(Puppet[:filetimeout])
+ end
+
it "should use the default environment if no name is provided while initializing an environment" do
Puppet.settings.expects(:value).with(:environment).returns("one")
Puppet::Node::Environment.new().name.should == :one