From 2b5d9e5f39e72253792716d50a63a43bc29c2c94 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sat, 14 Dec 2013 11:50:57 -0800 Subject: [PATCH] Add 'cache_lambda_templates' config option. Stop caching lambda templates by default, as they are generally too dynamic. Make lambda template caching an explicit opt-in. This will prevent filling cache directories with unusable files, and will actually speed things up the first time any given lambda template is used. Fixes #180 --- src/Mustache/Engine.php | 46 +++++++++++++++++++++++++++++-- test/Mustache/Test/EngineTest.php | 27 ++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/Mustache/Engine.php b/src/Mustache/Engine.php index d03d479..6d7ab98 100644 --- a/src/Mustache/Engine.php +++ b/src/Mustache/Engine.php @@ -34,6 +34,8 @@ class Mustache_Engine // Environment private $templateClassPrefix = '__Mustache_'; private $cache; + private $lambdaCache; + private $cacheLambdaTemplates = false; private $loader; private $partialsLoader; private $helpers; @@ -60,6 +62,10 @@ class Mustache_Engine * // *strongly* recommended that you configure your umask properly rather than overriding permissions here. * 'cache_file_mode' => 0666, * + * // Optionally, enable caching for lambda section templates. This is generally not recommended, as lambda + * // sections are often too dynamic to benefit from caching. + * 'cache_lambda_templates' => true, + * * // A Mustache template loader instance. Uses a StringLoader if not specified. * 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'), * @@ -122,6 +128,10 @@ class Mustache_Engine $this->setCache($cache); } + if (isset($options['cache_lambda_templates'])) { + $this->cacheLambdaTemplates = (bool) $options['cache_lambda_templates']; + } + if (isset($options['loader'])) { $this->setLoader($options['loader']); } @@ -516,6 +526,28 @@ class Mustache_Engine return $this->cache; } + /** + * Get the current Lambda Cache instance. + * + * If 'cache_lambda_templates' is enabled, this is the default cache instance. Otherwise, it is a NoopCache. + * + * @see Mustache_Engine::getCache + * + * @return Mustache_Cache + */ + protected function getLambdaCache() + { + if ($this->cacheLambdaTemplates) { + return $this->getCache(); + } + + if (!isset($this->lambdaCache)) { + $this->lambdaCache = new Mustache_Cache_NoopCache(); + } + + return $this->lambdaCache; + } + /** * Helper method to generate a Mustache template class. * @@ -597,25 +629,33 @@ class Mustache_Engine $source = $delims . "\n" . $source; } - return $this->loadSource($source); + return $this->loadSource($source, $this->getLambdaCache()); } /** * Instantiate and return a Mustache Template instance by source. * + * Optionally provide a Mustache_Cache instance. This is used internally by Mustache_Engine::loadLambda to respect + * the 'cache_lambda_templates' configuration option. + * * @see Mustache_Engine::loadTemplate * @see Mustache_Engine::loadPartial * @see Mustache_Engine::loadLambda * - * @param string $source + * @param string $source + * @param Mustache_Cache $cache (default: null) * * @return Mustache_Template */ - private function loadSource($source) + private function loadSource($source, Mustache_Cache $cache = null) { $className = $this->getTemplateClassName($source); if (!isset($this->templates[$className])) { + if ($cache === null) { + $cache = $this->getCache(); + } + if (!class_exists($className, false)) { if (!$this->getCache()->load($className)) { $compiled = $this->compile($source); diff --git a/test/Mustache/Test/EngineTest.php b/test/Mustache/Test/EngineTest.php index 81b72d1..1c71158 100644 --- a/test/Mustache/Test/EngineTest.php +++ b/test/Mustache/Test/EngineTest.php @@ -144,6 +144,27 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase $this->assertInstanceOf($className, $template); } + public function testLambdaCache() + { + $mustache = new MustacheStub(array( + 'cache' => self::$tempDir, + 'cache_lambda_templates' => true, + )); + + $this->assertNotInstanceOf('Mustache_Cache_NoopCache', $mustache->getProtectedLambdaCache()); + $this->assertSame($mustache->getCache(), $mustache->getProtectedLambdaCache()); + } + + public function testWithoutLambdaCache() + { + $mustache = new MustacheStub(array( + 'cache' => self::$tempDir + )); + + $this->assertInstanceOf('Mustache_Cache_NoopCache', $mustache->getProtectedLambdaCache()); + $this->assertNotSame($mustache->getCache(), $mustache->getProtectedLambdaCache()); + } + /** * @expectedException Mustache_Exception_InvalidArgumentException * @dataProvider getBadEscapers @@ -352,10 +373,16 @@ class MustacheStub extends Mustache_Engine { public $source; public $template; + public function loadTemplate($source) { $this->source = $source; return $this->template; } + + public function getProtectedLambdaCache() + { + return $this->getLambdaCache(); + } }