function DynamicPageCacheSubscriber::shouldCacheResponse

Same name in other branches
  1. 9 core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::shouldCacheResponse()
  2. 8.9.x core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::shouldCacheResponse()
  3. 11.x 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 237

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'];
    // Create a new CacheableMetadata to avoid changing the response itself.
    $cacheability = CacheableMetadata::createFromObject($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;
    }
    // Optimize the contexts and let them affect the cache tags to mimic what
    // happens to the cacheability in the variation cache.
    $cacheability->addCacheableDependency($this->cacheContextsManager
        ->convertTokensToKeys($cacheability->getCacheContexts()));
    $cacheability->setCacheContexts($this->cacheContextsManager
        ->optimizeTokens($cacheability->getCacheContexts()));
    // 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.