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

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
2016-10-29 09:03:28
Comments
Leave a Comment

Please login to continue.