diff --git a/core/lib/Drupal/Core/Template/TwigCacheChain.php b/core/lib/Drupal/Core/Template/TwigCacheChain.php deleted file mode 100644 index fc5c41b..0000000 --- a/core/lib/Drupal/Core/Template/TwigCacheChain.php +++ /dev/null @@ -1,87 +0,0 @@ -caches = $caches; - } - - /** - * {@inheritdoc} - */ - public function generateKey($name, $className) { - foreach ($this->caches as $cache) { - $key = $cache->generateKey($name, $className); - return $key; - } - } - - /** - * {@inheritdoc} - */ - public function has($key) { - foreach ($this->caches as $cache) { - if ($has = $cache->has($key)) { - return $has; - } - } - } - - /** - * {@inheritdoc} - */ - public function load($key) { - foreach ($this->caches as $cache) { - if ($cache->has($key)) { - $cache->load($key); - return; - } - } - } - - /** - * {@inheritdoc} - */ - public function write($key, $content) { - // We want to write to all of them. - foreach ($this->caches as $cache) { - $cache->write($key, $content); - } - } - - /** - * {@inheritdoc} - */ - public function getTimestamp($key) { - foreach ($this->caches as $cache) { - if ($timestamp = $cache->getTimestamp($key)) { - return $timestamp; - } - } - } - -} diff --git a/core/lib/Drupal/Core/Template/TwigCacheNull.php b/core/lib/Drupal/Core/Template/TwigCacheNull.php deleted file mode 100644 index 0449a34..0000000 --- a/core/lib/Drupal/Core/Template/TwigCacheNull.php +++ /dev/null @@ -1,40 +0,0 @@ -buffer[$key]); - } - - /** - * {@inheritdoc} - */ - public function write($key, $content) { - $this->buffer[$key] = $content; - } - - /** - * {@inheritdoc} - */ - public function load($key) { - eval('?>' . $this->buffer[$key]); - - unset($this->buffer[$key]); - } - -} diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php index e59b4ac..845f219 100644 --- a/core/lib/Drupal/Core/Template/TwigEnvironment.php +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php @@ -59,14 +59,8 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension // Ensure autoescaping is always on. $options['autoescape'] = 'html'; - // We set up a chain of cache classes in case there is a failure or race - // condition. The null cache is roughly equivalent to disabling the Twig - // cache entirely. - $php_storage_cache = new TwigPhpStorageCache($cache, $twig_extension_hash); - $null_cache = new TwigCacheNull(); - if ($options['cache'] === TRUE) { - $options['cache'] = new TwigCacheChain([$php_storage_cache, $null_cache]); + $options['cache'] = new TwigPhpStorageCache($cache, $twig_extension_hash); } $this->loader = $loader; diff --git a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php index 44bbd27..1f5e77c 100644 --- a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php +++ b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php @@ -37,6 +37,13 @@ class TwigPhpStorageCache implements \Twig_CacheInterface { protected $templateCacheFilenamePrefix; /** + * Template source cache for when the template cannot be loaded from storage. + * + * @var array + */ + protected $templateSourceCache = []; + + /** * Store cache backend and other information internally. * * @param \Drupal\Core\Cache\CacheBackendInterface $cache @@ -87,8 +94,14 @@ public function has($key) { * {@inheritdoc} */ public function load($key) { - if (!$this->storage()->load($key)) { - throw new \RuntimeException(sprintf('Failed to load Twig cache file "%s".', $key)); + // if (!$this->storage()->load($key)) { + if (TRUE) { + // eval() the stored template since we were not able to load it from + // storage. + // + // This only works when the PhpStorage cache is cold, it does not help us + // if has() returns TRUE because then write() is not called. :( + eval('?>' . $this->templateSourceCache[$key]); } } @@ -96,9 +109,9 @@ public function load($key) { * {@inheritdoc} */ public function write($key, $content) { - if (!$this->storage()->save($key, $content)) { - throw new \RuntimeException(sprintf('Failed to write Twig cache file "%s".', $key)); - } + $this->storage()->save($key, $content); + // Store the compiled template in case loading from PhpStorage fails. + $this->templateSourceCache[$key] = $content; // Save the last mtime. $cid = 'twig:' . $key; $this->cache->set($cid, REQUEST_TIME);