DynamicPageCacheSubscriber::shouldCacheResponse

protected DynamicPageCacheSubscriber::shouldCacheResponse(CacheableResponseInterface $response)

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()

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.