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:
+43
-3
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user