You are here

protected function DynamicPageCacheSubscriber::shouldCacheResponse in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::shouldCacheResponse()
  2. 9 core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::shouldCacheResponse()

Whether the given response should be cached by Dynamic Page Cache.

We consider any response that has cacheability metadata meeting the auto- placeholdering conditions to be uncacheable. Because those conditions indicate poor cacheability, and if it doesn't make sense to cache parts of a page, then neither does it make sense to cache an entire page.

But note that auto-placeholdering avoids such cacheability metadata ever bubbling to the response level: while rendering, the Renderer checks every subtree to see if meets the auto-placeholdering conditions. If it does, it is automatically placeholdered, and consequently the cacheability metadata of the placeholdered content does not bubble up to the response level.

Parameters

\Drupal\Core\Cache\CacheableResponseInterface $response: The response whose cacheability to analyze.

Return value

bool Whether the given response should be cached.

See also

\Drupal\Core\Render\Renderer::shouldAutomaticallyPlaceholder()

1 call to DynamicPageCacheSubscriber::shouldCacheResponse()
DynamicPageCacheSubscriber::onResponse in core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
Stores a response in case of a Dynamic Page Cache miss, if cacheable.

File

core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php, line 229

Class

DynamicPageCacheSubscriber
Returns cached responses as early and avoiding as much work as possible.

Namespace

Drupal\dynamic_page_cache\EventSubscriber

Code

protected function shouldCacheResponse(CacheableResponseInterface $response) {
  $conditions = $this->rendererConfig['auto_placeholder_conditions'];
  $cacheability = $response
    ->getCacheableMetadata();

  // Response's max-age is at or below the configured threshold.
  if ($cacheability
    ->getCacheMaxAge() !== Cache::PERMANENT && $cacheability
    ->getCacheMaxAge() <= $conditions['max-age']) {
    return FALSE;
  }

  // Response has a high-cardinality cache context.
  if (array_intersect($cacheability
    ->getCacheContexts(), $conditions['contexts'])) {
    return FALSE;
  }

  // Response has a high-invalidation frequency cache tag.
  if (array_intersect($cacheability
    ->getCacheTags(), $conditions['tags'])) {
    return FALSE;
  }
  return TRUE;
}