core/lib/Drupal/Core/Render/RenderCache.php | 28 +++++++++++----- .../Tests/Core/Render/RendererBubblingTest.php | 39 ++++++++++++++-------- .../Tests/Core/Render/RendererPlaceholdersTest.php | 6 ++-- .../Drupal/Tests/Core/Render/RendererTestBase.php | 7 ++++ 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/core/lib/Drupal/Core/Render/RenderCache.php b/core/lib/Drupal/Core/Render/RenderCache.php index 880fca3..339f69c 100644 --- a/core/lib/Drupal/Core/Render/RenderCache.php +++ b/core/lib/Drupal/Core/Render/RenderCache.php @@ -227,6 +227,16 @@ public function set(array &$elements, array $pre_bubbling_elements) { // redirect, because the last update's max-age would always "win".) $redirect_cacheability_updated = CacheableMetadata::createFromRenderArray($data)->merge($redirect_cacheability); + // Create a redirect cache tag, which is used for two purposes: + // 1. invalidate cached supertrees (otherwise redirected render cache + // items may continue to not be automatically placeholdered, because a + // high-cardinality cache context was not yet known originally) + // 2. invalidate cache items that become obsolete when the cache contexts + // stored in the cache redirect are discovered to be incomplete. + $redirect_cache_tag = 'render_cache_redirect:' . hash('sha1', serialize([$pre_bubbling_elements['#cache']['keys'], $pre_bubbling_elements['#cache']['contexts'], $bin])); + $redirect_cacheability_updated->addCacheTags([$redirect_cache_tag]); + $data['#cache']['tags'] = Cache::mergeTags($data['#cache']['tags'], [$redirect_cache_tag]); + // Stored cache contexts incomplete: this request causes cache contexts to // be added to the redirecting cache item. if (array_diff($redirect_cacheability_updated->getCacheContexts(), $redirect_cacheability->getCacheContexts())) { @@ -246,6 +256,9 @@ public function set(array &$elements, array $pre_bubbling_elements) { 'bin' => $bin, ], ]; + // First invalidate the cache tag for this redirect, then set the new + // version of this redirect. + Cache::invalidateTags([$redirect_cache_tag]); $cache->set($pre_bubbling_cid, $redirect_data, $this->maxAgeToExpire($redirect_cacheability_updated->getCacheMaxAge()), Cache::mergeTags($redirect_data['#cache']['tags'], ['rendered'])); } @@ -255,16 +268,13 @@ public function set(array &$elements, array $pre_bubbling_elements) { // redirecting cache item would be pointing to a cache item that can never // exist. if (array_diff($redirect_cacheability_updated->getCacheContexts(), $data['#cache']['contexts'])) { - // Recalculate the cache ID. - $recalculated_cid_pseudo_element = [ - '#cache' => [ - 'keys' => $elements['#cache']['keys'], - 'contexts' => $redirect_cacheability_updated->getCacheContexts(), - ] - ]; - $cid = $this->createCacheID($recalculated_cid_pseudo_element); - // Ensure the about-to-be-cached data uses the merged cache contexts. + // Ensure the about-to-be-cached data uses the merged cache contexts and + // the redirect cache tag. + $data['#cache']['keys'] = $elements['#cache']['keys']; $data['#cache']['contexts'] = $redirect_cacheability_updated->getCacheContexts(); + // Recalculate the cache ID. + $cid = $this->createCacheID($data); + $data = $this->getCacheableRenderArray($data); } } $cache->set($cid, $data, $this->maxAgeToExpire($elements['#cache']['max-age']), Cache::mergeTags($data['#cache']['tags'], ['rendered'])); diff --git a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php index b9bb4e5..7a4c77a 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php @@ -86,6 +86,13 @@ public function testContextBubblingCustomCacheBin() { $this->setUpRequest(); $this->memoryCache = new MemoryBackend('render'); $custom_cache = new MemoryBackend($bin); + $expected_redirect_cache_tag = 'render_cache_redirect:' . hash('sha1', serialize([['parent'], ['foo'], $bin])); + + // The cache redirect will be updated once, hence its cache tag should be + // invalidated once as well. + $this->cacheTagsInvalidator->expects($this->once()) + ->method('invalidateTags') + ->with([$expected_redirect_cache_tag]); $this->cacheFactory->expects($this->atLeastOnce()) ->method('get') @@ -123,7 +130,7 @@ public function testContextBubblingCustomCacheBin() { '#cache' => [ 'keys' => ['parent'], 'contexts' => ['bar', 'foo'], - 'tags' => [], + 'tags' => [$expected_redirect_cache_tag], 'bin' => $bin, 'max-age' => 3600, ], @@ -278,7 +285,7 @@ public function providerTestContextBubblingEdgeCases() { // The keys + contexts this redirects to. 'keys' => ['parent'], 'contexts' => ['bar', 'foo'], - 'tags' => ['dee', 'fiddle', 'har', 'yar'], + 'tags' => ['dee', 'fiddle', 'har', 'render_cache_redirect:02130855cf57da2054a67bdbf19a782b9d6f655f', 'yar'], 'bin' => 'render', 'max-age' => Cache::PERMANENT, ], @@ -287,7 +294,7 @@ public function providerTestContextBubblingEdgeCases() { '#attached' => [], '#cache' => [ 'contexts' => ['bar', 'foo'], - 'tags' => ['dee', 'fiddle', 'har', 'yar'], + 'tags' => ['dee', 'fiddle', 'har', 'render_cache_redirect:02130855cf57da2054a67bdbf19a782b9d6f655f', 'yar'], 'max-age' => Cache::PERMANENT, ], '#markup' => 'parent', @@ -307,6 +314,12 @@ public function testConditionalCacheContextBubblingSelfHealing() { $this->setUpRequest(); $this->setupMemoryCache(); + // The cache redirect will be updated three times, and hence its cache tag + // will also be invalidated three times. + $this->cacheTagsInvalidator->expects($this->exactly(3)) + ->method('invalidateTags') + ->with(['render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f']); + $test_element = [ '#cache' => [ 'keys' => ['parent'], @@ -355,7 +368,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#cache' => [ 'keys' => ['parent'], 'contexts' => ['user.roles'], - 'tags' => ['a', 'b'], + 'tags' => ['a', 'b', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'bin' => 'render', 'max-age' => Cache::PERMANENT, ], @@ -364,7 +377,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#attached' => [], '#cache' => [ 'contexts' => ['user.roles'], - 'tags' => ['a', 'b'], + 'tags' => ['a', 'b', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'max-age' => Cache::PERMANENT, ], '#markup' => 'parent', @@ -380,7 +393,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#cache' => [ 'keys' => ['parent'], 'contexts' => ['foo', 'user.roles'], - 'tags' => ['a', 'b', 'c'], + 'tags' => ['a', 'b', 'c', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'bin' => 'render', 'max-age' => 1800, ], @@ -389,7 +402,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#attached' => [], '#cache' => [ 'contexts' => ['foo', 'user.roles'], - 'tags' => ['a', 'b', 'c'], + 'tags' => ['a', 'b', 'c', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'max-age' => 1800, ], '#markup' => 'parent', @@ -413,7 +426,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#cache' => [ 'keys' => ['parent'], 'contexts' => ['foo', 'user.roles'], - 'tags' => ['a', 'b', 'c'], + 'tags' => ['a', 'b', 'c', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'bin' => 'render', 'max-age' => 1800, ], @@ -422,7 +435,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#attached' => [], '#cache' => [ 'contexts' => ['foo', 'user.roles'], - 'tags' => ['a', 'b'], + 'tags' => ['a', 'b', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], // Note that the max-age here is unaffected. When role A, the grandchild // is never rendered, so neither is its max-age of 1800 present here, // despite 1800 being the max-age of the redirecting cache item. @@ -442,7 +455,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#cache' => [ 'keys' => ['parent'], 'contexts' => ['bar', 'foo', 'user.roles'], - 'tags' => ['a', 'b', 'c', 'd'], + 'tags' => ['a', 'b', 'c', 'd', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'bin' => 'render', 'max-age' => 300, ], @@ -452,7 +465,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#attached' => [], '#cache' => [ 'contexts' => ['bar', 'foo', 'user.roles'], - 'tags' => ['a', 'b', 'c', 'd'], + 'tags' => ['a', 'b', 'c', 'd', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], 'max-age' => 300, ], '#markup' => 'parent', @@ -467,7 +480,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#attached' => [], '#cache' => [ 'contexts' => ['bar', 'foo', 'user.roles'], - 'tags' => ['a', 'b'], + 'tags' => ['a', 'b', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], // Note that the max-age here is unaffected. When role A, the grandchild // is never rendered, so neither is its max-age of 1800 present here, // nor the grandgrandchild's max-age of 300, despite 300 being the @@ -486,7 +499,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { '#attached' => [], '#cache' => [ 'contexts' => ['bar', 'foo', 'user.roles'], - 'tags' => ['a', 'b', 'c'], + 'tags' => ['a', 'b', 'c', 'render_cache_redirect:46bb17a6d36ce310c5b8c747c3d84a44f29a703f'], // Note that the max-age here is unaffected. When role B, the // grandgrandchild is never rendered, so neither is its max-age of 300 // present here, despite 300 being the max-age of the redirecting cache diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php index e33a021..d4d7d09 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php @@ -371,7 +371,7 @@ public function providerPlaceholders() { ], '#cache' => [ 'contexts' => ['user'], - 'tags' => [], + 'tags' => ['render_cache_redirect:ce7e10fb433d1b051ff2e85e6210d6402701e5c0'], 'max-age' => Cache::PERMANENT, ], ], @@ -568,7 +568,7 @@ public function testCacheableParent($test_element, $args, array $expected_placeh '#cache' => [ 'keys' => ['placeholder_test_GET'], 'contexts' => ['user'], - 'tags' => [], + 'tags' => ['render_cache_redirect:da9373df59ce294094c3ef12ffb5294c982e5b69'], 'max-age' => Cache::PERMANENT, 'bin' => 'render', ], @@ -586,7 +586,7 @@ public function testCacheableParent($test_element, $args, array $expected_placeh ], '#cache' => [ 'contexts' => $bubbled_cache_contexts, - 'tags' => [], + 'tags' => ['render_cache_redirect:da9373df59ce294094c3ef12ffb5294c982e5b69'], 'max-age' => Cache::PERMANENT, ], ]; diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php index 2623e39..3051c42 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -54,6 +54,11 @@ class RendererTestBase extends UnitTestCase { protected $cacheContexts; /** + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheTagsInvalidator; + + /** * The mocked controller resolver. * * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject @@ -144,9 +149,11 @@ protected function setUp() { }); $this->renderCache = new PlaceholderingRenderCache($this->requestStack, $this->cacheFactory, $this->cacheContextsManager, $this->rendererConfig); $this->renderer = new Renderer($this->controllerResolver, $this->themeManager, $this->elementInfo, $this->renderCache, $this->requestStack, $this->rendererConfig); + $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); $container = new ContainerBuilder(); $container->set('cache_contexts_manager', $this->cacheContextsManager); + $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); $container->set('render_cache', $this->renderCache); $container->set('renderer', $this->renderer); \Drupal::setContainer($container);