Load templates from cache, even if they have just been compiled

This commit is contained in:
Matthias Pigulla
2016-11-03 10:27:16 +01:00
committed by Fabien Potencier
parent eeeb1c35f8
commit ff0abbb7e8
2 changed files with 16 additions and 4 deletions
+9 -1
View File
@@ -416,9 +416,17 @@ class Twig_Environment
$this->writeCacheFile($key, $content); $this->writeCacheFile($key, $content);
} else { } else {
$this->cache->write($key, $content); $this->cache->write($key, $content);
$this->cache->load($key);
} }
eval('?>'.$content); if (!class_exists($cls, false)) {
/* Last line of defense if either $this->bcWriteCacheFile was used,
* $this->cache is implemented as a no-op or we have a race condition
* where the cache was cleared between the above calls to write to and load from
* the cache.
*/
eval('?>'.$content);
}
} }
} }
+7 -3
View File
@@ -217,7 +217,9 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
->will($this->returnValue(0)); ->will($this->returnValue(0));
$loader->expects($this->never()) $loader->expects($this->never())
->method('isFresh'); ->method('isFresh');
$cache->expects($this->never()) $cache->expects($this->once())
->method('write');
$cache->expects($this->once())
->method('load'); ->method('load');
$twig->loadTemplate($templateName); $twig->loadTemplate($templateName);
@@ -245,7 +247,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$loader->expects($this->once()) $loader->expects($this->once())
->method('isFresh') ->method('isFresh')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$cache->expects($this->once()) $cache->expects($this->atLeastOnce())
->method('load'); ->method('load');
$twig->loadTemplate($templateName); $twig->loadTemplate($templateName);
@@ -271,7 +273,9 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$loader->expects($this->once()) $loader->expects($this->once())
->method('isFresh') ->method('isFresh')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$cache->expects($this->never()) $cache->expects($this->once())
->method('write');
$cache->expects($this->once())
->method('load'); ->method('load');
$twig->loadTemplate($templateName); $twig->loadTemplate($templateName);