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
This commit is contained in:
Justin Hileman
2013-12-14 11:50:57 -08:00
parent 6b7f33c78d
commit 2b5d9e5f39
2 changed files with 70 additions and 3 deletions
+42 -2
View File
@@ -34,6 +34,8 @@ class Mustache_Engine
// Environment // Environment
private $templateClassPrefix = '__Mustache_'; private $templateClassPrefix = '__Mustache_';
private $cache; private $cache;
private $lambdaCache;
private $cacheLambdaTemplates = false;
private $loader; private $loader;
private $partialsLoader; private $partialsLoader;
private $helpers; private $helpers;
@@ -60,6 +62,10 @@ class Mustache_Engine
* // *strongly* recommended that you configure your umask properly rather than overriding permissions here. * // *strongly* recommended that you configure your umask properly rather than overriding permissions here.
* 'cache_file_mode' => 0666, * '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. * // A Mustache template loader instance. Uses a StringLoader if not specified.
* 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'), * 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),
* *
@@ -122,6 +128,10 @@ class Mustache_Engine
$this->setCache($cache); $this->setCache($cache);
} }
if (isset($options['cache_lambda_templates'])) {
$this->cacheLambdaTemplates = (bool) $options['cache_lambda_templates'];
}
if (isset($options['loader'])) { if (isset($options['loader'])) {
$this->setLoader($options['loader']); $this->setLoader($options['loader']);
} }
@@ -516,6 +526,28 @@ class Mustache_Engine
return $this->cache; 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. * Helper method to generate a Mustache template class.
* *
@@ -597,25 +629,33 @@ class Mustache_Engine
$source = $delims . "\n" . $source; $source = $delims . "\n" . $source;
} }
return $this->loadSource($source); return $this->loadSource($source, $this->getLambdaCache());
} }
/** /**
* Instantiate and return a Mustache Template instance by source. * 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::loadTemplate
* @see Mustache_Engine::loadPartial * @see Mustache_Engine::loadPartial
* @see Mustache_Engine::loadLambda * @see Mustache_Engine::loadLambda
* *
* @param string $source * @param string $source
* @param Mustache_Cache $cache (default: null)
* *
* @return Mustache_Template * @return Mustache_Template
*/ */
private function loadSource($source) private function loadSource($source, Mustache_Cache $cache = null)
{ {
$className = $this->getTemplateClassName($source); $className = $this->getTemplateClassName($source);
if (!isset($this->templates[$className])) { if (!isset($this->templates[$className])) {
if ($cache === null) {
$cache = $this->getCache();
}
if (!class_exists($className, false)) { if (!class_exists($className, false)) {
if (!$this->getCache()->load($className)) { if (!$this->getCache()->load($className)) {
$compiled = $this->compile($source); $compiled = $this->compile($source);
+27
View File
@@ -144,6 +144,27 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
$this->assertInstanceOf($className, $template); $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 * @expectedException Mustache_Exception_InvalidArgumentException
* @dataProvider getBadEscapers * @dataProvider getBadEscapers
@@ -352,10 +373,16 @@ class MustacheStub extends Mustache_Engine
{ {
public $source; public $source;
public $template; public $template;
public function loadTemplate($source) public function loadTemplate($source)
{ {
$this->source = $source; $this->source = $source;
return $this->template; return $this->template;
} }
public function getProtectedLambdaCache()
{
return $this->getLambdaCache();
}
} }