class DynamicPageCacheSubscriber implements EventSubscriberInterface (View source)

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

Dynamic Page Cache is able to cache so much because it utilizes cache contexts: the cache contexts that are present capture the variations of every component of the page. That, combined with the fact that cacheability metadata is bubbled, means that the cache contexts at the page level represent the complete set of contexts that the page varies by.

The reason Dynamic Page Cache is implemented as two event subscribers (a late REQUEST subscriber immediately after routing for cache hits, and an early RESPONSE subscriber for cache misses) is because many cache contexts can only be evaluated after routing. (Examples: 'user', 'user.permissions', 'route' …) Consequently, it is impossible to implement Dynamic Page Cache as a kernel middleware that simply caches per URL.

Constants

HEADER

Name of Dynamic Page Cache's response header.

Properties

protected RequestPolicyInterface $requestPolicy

A request policy rule determining the cacheability of a response.

protected ResponsePolicyInterface $responsePolicy

A response policy rule determining the cacheability of the response.

protected RenderCacheInterface $renderCache

The render cache.

protected array $rendererConfig

The renderer configuration array.

protected array $dynamicPageCacheRedirectRenderArray

Dynamic Page Cache's redirect render array.

protected SplObjectStorage $requestPolicyResults

Internal cache of request policy results.

Methods

__construct(RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, RenderCacheInterface $render_cache, array $renderer_config)

Constructs a new DynamicPageCacheSubscriber object.

onRequest(GetResponseEvent $event)

Sets a response in case of a Dynamic Page Cache hit.

onResponse(FilterResponseEvent $event)

Stores a response in case of a Dynamic Page Cache miss, if cacheable.

bool
shouldCacheResponse(CacheableResponseInterface $response)

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

array
responseToRenderArray(CacheableResponseInterface $response)

Embeds a Response object in a render array so that RenderCache can cache it.

renderArrayToResponse(array $render_array)

Gets the embedded Response object in a render array.

static 
getSubscribedEvents()

{@inheritdoc}

Details

__construct(RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, RenderCacheInterface $render_cache, array $renderer_config)

Constructs a new DynamicPageCacheSubscriber object.

Parameters

RequestPolicyInterface $request_policy

A policy rule determining the cacheability of a request.

ResponsePolicyInterface $response_policy

A policy rule determining the cacheability of the response.

RenderCacheInterface $render_cache

The render cache.

array $renderer_config

The renderer configuration array.

onRequest(GetResponseEvent $event)

Sets a response in case of a Dynamic Page Cache hit.

Parameters

GetResponseEvent $event

The event to process.

onResponse(FilterResponseEvent $event)

Stores a response in case of a Dynamic Page Cache miss, if cacheable.

Parameters

FilterResponseEvent $event

The event to process.

protected bool 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

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

protected array responseToRenderArray(CacheableResponseInterface $response)

Embeds a Response object in a render array so that RenderCache can cache it.

Refactor/remove once https://www.drupal.org/node/2551419 lands.

Parameters

CacheableResponseInterface $response

A cacheable response.

Return Value

array

A render array that embeds the given cacheable response object, with the cacheability metadata of the response object present in the #cache property of the render array.

See also

renderArrayToResponse()

protected CacheableResponseInterface renderArrayToResponse(array $render_array)

Gets the embedded Response object in a render array.

Parameters

array $render_array

A render array with a #response property.

Return Value

CacheableResponseInterface

The cacheable response object.

See also

responseToRenderArray()

static getSubscribedEvents()

{@inheritdoc}