.../Core/Render/PlaceholderingRenderCache.php | 54 +++++++++++++++ core/lib/Drupal/Core/Render/RenderCache.php | 40 +++++++++-- .../Tests/Core/Render/RendererPlaceholdersTest.php | 80 +++++++++++++++++++++- .../Drupal/Tests/Core/Render/RendererTestBase.php | 15 ++++ 4 files changed, 179 insertions(+), 10 deletions(-) diff --git a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php index 1ae93ae..461e5c1 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php +++ b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php @@ -56,6 +56,16 @@ class PlaceholderingRenderCache extends RenderCache { protected $rendererConfig; /** + * The expire override. + * + * @see ::set() + * @see ::maxAgeToOverride() + * + * @var FALSE|int + */ + protected $expireOverride = FALSE; + + /** * Constructs a new PlaceholderingRenderCache object. * * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack @@ -86,6 +96,17 @@ public function get(array $elements) { return $this->createPlaceholder($elements); } + // PlaceholderingRenderCache::set() has cached a bubbled max-age = 0, and + // placeholders are currently being replaced. Don't return the cached + // element, because it *only* contains max-age = 0, it doesn't contain the + // rendered content (which it cannot and may not, because after all it was + // uncacheable, due to that max-age = 0). + // When not replacing placeholders, the above will ensure that this + // max-age=0 element is placeholdered. + if (isset($elements['#create_placeholder']) && $elements['#create_placeholder'] === FALSE && !$this->maxAgeAllowsCaching($cached_element)) { + return FALSE; + } + return $cached_element; } } @@ -96,6 +117,27 @@ public function get(array $elements) { public function set(array &$elements, array $pre_bubbling_elements) { $result = parent::set($elements, $pre_bubbling_elements); + // If the parent's set() call did not result in a cache write, and that was + // the case due to a max-age=0 that bubbled for a #lazy_builder's contents, + // then cache the fact that it was a bubbled max-age=0. Don't cache markup + // or anything else: *only* cache max-age=0. + // The accompanying logic in PlaceholderingRenderCache::get() will ensure + // the render cached item is never actually returned; it will only be used + // by PlaceholderingRenderCache::get() to automatically placeholder this + // element whose subtree is uncacheable, and whose contents are generated by + // a #lazy_builder callback. + // Note: we can permanently cache the fact that this element's subtree + // resulted in a bubbled max-age=0, because cache context variations + if ($result === FALSE && $this->canCreatePlaceholder($pre_bubbling_elements) && $this->requestStack->getCurrentRequest()->isMethodSafe() && !$this->maxAgeAllowsCaching($elements) && $this->maxAgeAllowsCaching($pre_bubbling_elements) && $cid = $this->createCacheID($elements)) { + // Cache #cache['max-age'] = 0! We just want to cache the cacheability. + $data = ['#cache' => $elements['#cache'], '#markup' => '', '#attached' => []]; + + // + $this->expireOverride = Cache::PERMANENT; + parent::doSet($data, $pre_bubbling_elements); + $this->expireOverride = FALSE; + } + if ($this->canCreatePlaceholder($pre_bubbling_elements) && $this->shouldAutomaticallyPlaceholder($elements)) { // Overwrite $elements with a placeholder. The Renderer (which called this // method) will update the context with the bubbleable metadata of the @@ -138,6 +180,18 @@ protected function shouldAutomaticallyPlaceholder(array $element) { } /** + * {@inheritdoc} + */ + protected function maxAgeToExpire($max_age) { + // This is a quite hacky override, but otherwise this would have to + // reimplement all of \Drupal\Core\Render\RenderCache::doSet(). + if ($this->expireOverride !== FALSE) { + return $this->expireOverride; + } + return parent::maxAgeToExpire($max_age); + } + + /** * Analyzes whether the given render array can be placeholdered. * * @param array $element diff --git a/core/lib/Drupal/Core/Render/RenderCache.php b/core/lib/Drupal/Core/Render/RenderCache.php index 339f69c..abd37dc 100644 --- a/core/lib/Drupal/Core/Render/RenderCache.php +++ b/core/lib/Drupal/Core/Render/RenderCache.php @@ -64,10 +64,11 @@ public function get(array $elements) { // and render caching of forms prevents this from happening. // @todo remove the isMethodSafe() check when // https://www.drupal.org/node/2367555 lands. - if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = $this->createCacheID($elements)) { + if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$this->maxAgeAllowsCaching($elements) || !isset($elements['#cache']['keys'])) { return FALSE; } $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render'; + $cid = $this->createCacheID($elements); if (!empty($cid) && ($cache_bin = $this->cacheFactory->get($bin)) && $cache = $cache_bin->get($cid)) { $cached_element = $cache->data; @@ -91,10 +92,24 @@ public function set(array &$elements, array $pre_bubbling_elements) { // and render caching of forms prevents this from happening. // @todo remove the isMethodSafe() check when // https://www.drupal.org/node/2367555 lands. - if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = $this->createCacheID($elements)) { + if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !isset($elements['#cache']['keys']) || !$this->maxAgeAllowsCaching($elements)) { return FALSE; } + return $this->doSet($elements, $pre_bubbling_elements); + } + + /** + * Helper for ::set(). + * + * @param array $elements + * Unlike ::set(), this is not passed by reference, which means side-effects + * do not exist anymore here; they're isolated to set(). + * @param array $pre_bubbling_elements + */ + protected function doSet(array $elements, array $pre_bubbling_elements) { + $cid = $this->createCacheID($elements); + $data = $this->getCacheableRenderArray($elements); $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render'; @@ -296,6 +311,22 @@ protected function maxAgeToExpire($max_age) { } /** + * If the maximum age is zero, then caching is effectively prohibited. + * + * @param array $elements + * A render array. + * + * @return bool + * Whether this render array has a non-zero max-age. + */ + protected function maxAgeAllowsCaching(array $elements) { + if (isset($elements['#cache']['max-age']) && $elements['#cache']['max-age'] === 0) { + return FALSE; + } + return TRUE; + } + + /** * Creates the cache ID for a renderable element. * * Creates the cache ID string based on #cache['keys'] + #cache['contexts']. @@ -307,11 +338,6 @@ protected function maxAgeToExpire($max_age) { * The cache ID string, or FALSE if the element may not be cached. */ protected function createCacheID(array &$elements) { - // If the maximum age is zero, then caching is effectively prohibited. - if (isset($elements['#cache']['max-age']) && $elements['#cache']['max-age'] === 0) { - return FALSE; - } - if (isset($elements['#cache']['keys'])) { $cid_parts = $elements['#cache']['keys']; if (!empty($elements['#cache']['contexts'])) { diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php index d0c6589..b1fcc4c 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php @@ -139,6 +139,22 @@ public function providerPlaceholders() { '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callback', $args], ], ]; + // Note the absence of '#create_placeholder', presence of max-age=0 created + // by the #lazy_builder callback. + // @see \Drupal\Tests\Core\Render\PlaceholdersTest::callbackMaxAgeZero() + $base_element_a5 = [ + '#attached' => [ + 'drupalSettings' => [ + 'foo' => 'bar', + ], + ], + 'placeholder' => [ + '#cache' => [ + 'contexts' => [], + ], + '#lazy_builder' => ['Drupal\Tests\Core\Render\PlaceholdersTest::callbackMaxAgeZero', $args], + ], + ]; // Note the absence of '#create_placeholder', presence of high cardinality // cache context created by the #lazy_builder callback. // @see \Drupal\Tests\Core\Render\PlaceholdersTest::callbackPerUser() @@ -359,6 +375,48 @@ public function providerPlaceholders() { ], ]; + // Case nine: render array that DOES NOT have a placeholder that is: + // - NOT created, despite max-age=0 that is bubbled + // - uncacheable + // (because the render element with #lazy_builder does not have #cache[keys] + // and hence the max-age=0 bubbles up further) + $element_without_cache_keys = $base_element_a5; + $expected_placeholder_render_array = $extract_placeholder_render_array($base_element_a5['placeholder']); + $cases[] = [ + $element_without_cache_keys, + $args, + $expected_placeholder_render_array, + FALSE, + [], + [], + [], + ]; + + // Case ten: render array that has a placeholder that is: + // - automatically created, and automatically triggered due to max-age=0 + // that is bubbled + // - cacheable + $element_with_cache_keys = $base_element_a5; + $element_with_cache_keys['placeholder']['#cache']['keys'] = $keys; + $expected_placeholder_render_array['#cache']['keys'] = $keys; + $cases[] = [ + $element_with_cache_keys, + $args, + $expected_placeholder_render_array, + $keys, + [], + [], + [ + '#markup' => '', + '#attached' => [], + '#cache' => [ + 'contexts' => [], + 'tags' => [], + 'max-age' => 0, + ], + ], + ]; + // Case eleven: render array that DOES NOT have a placeholder that is: // - NOT created, despite high cardinality cache contexts that are bubbled // - uncacheable @@ -549,6 +607,9 @@ protected function assertPlaceholderRenderCache($cid_parts, array $bubbled_cache $cached = $this->memoryCache->get(implode(':', array_merge($cid_parts, $bubbled_cache_contexts))); $cached_element = $cached->data; $this->assertEquals($expected_data, $cached_element, 'The correct data is cached: the stored #markup and #attached properties are not affected by the placeholder being replaced.'); + if ($expected_data['#cache']['max-age'] === 0) { + $this->assertEquals(-1, $cached->expire, 'When a bubbled max-age=0 is cached, it is cached permanently for the given set of '); + } } } /** @@ -625,16 +686,23 @@ public function testCacheableParent($test_element, $args, array $expected_placeh // Edge cases: always where both bubbling of an auto-placeholdering // condition happens from within a #lazy_builder that is uncacheable. // - uncacheable + A5 (cache max-age) - // @todo + $edge_case_a5_uncacheable = $has_uncacheable_lazy_builder && $test_element['placeholder']['#lazy_builder'][0] === 'Drupal\Tests\Core\Render\PlaceholdersTest::callbackMaxAgeZero'; // - uncacheable + A6 (cache context) $edge_case_a6_uncacheable = $has_uncacheable_lazy_builder && $test_element['placeholder']['#lazy_builder'][0] === 'Drupal\Tests\Core\Render\PlaceholdersTest::callbackPerUser'; // - uncacheable + A7 (cache tag) $edge_case_a7_uncacheable = $has_uncacheable_lazy_builder && $test_element['placeholder']['#lazy_builder'][0] === 'Drupal\Tests\Core\Render\PlaceholdersTest::callbackTagCurrentTemperature'; + // The completely uncacheable edge case: max-age=0 is bubbled from a + // #lazy_builder callback for an uncacheable placeholder. The element + // containing the uncacheable placeholder has cache keys set, but also does + // not cache it due to max-age=0. + if ($edge_case_a5_uncacheable) { + $this->assertFalse($cached, 'The parent is not render cached in case a max-age=0 is bubbled from an uncacheable child (no #cache[keys]) with a #lazy_builder.'); + } // The redirect-cacheable edge case: a high-cardinality cache context is // bubbled from a #lazy_builder callback for an uncacheable placeholder. The // element containing the uncacheable placeholder has cache keys set, and // due to the bubbled cache contexts it creates a cache redirect. - if ($edge_case_a6_uncacheable) { + elseif ($edge_case_a6_uncacheable) { $cached_element = $cached->data; $expected_redirect = [ '#cache_redirect' => TRUE, @@ -718,7 +786,13 @@ public function testCacheableParent($test_element, $args, array $expected_placeh $element['#prefix'] = '
#cache enabled, GET
'; $output = $this->renderer->renderRoot($element); $this->assertSame('#cache enabled, GET
This is a rendered placeholder!
', (string) $output, 'Output is overridden.'); - $this->assertFalse(isset($element['#printed']), 'Cache hit'); + if ($edge_case_a5_uncacheable ) { + $this->assertTrue(isset($element['#printed']), 'Cache miss, because this bubbles max-age=0 plus an uncached lazy builder, hence max-age=0 bubbles all the way to the parent.'); + } + // Regular case. + else { + $this->assertFalse(isset($element['#printed']), 'Cache hit'); + } $this->assertSame('#cache enabled, GET
This is a rendered placeholder!
', (string) $element['#markup'], '#markup is overridden.'); $expected_js_settings = [ 'foo' => 'bar', diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php index 5145d58..f3448df 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -261,6 +261,21 @@ public static function callback($animal, $use_animal_as_array_key = FALSE) { } /** + * #lazy_builder callback; attaches setting, generates markup, max-age = 0. + * + * @param string $animal + * An animal. + * + * @return array + * A renderable array. + */ + public static function callbackMaxAgeZero($animal) { + $build = static::callback($animal); + $build['#cache']['max-age'] = 0; + return $build; + } + + /** * #lazy_builder callback; attaches setting, generates markup, user-specific. * * @param string $animal