DynamicPageCacheSubscriber
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
Constructs a new DynamicPageCacheSubscriber object.
Sets a response in case of a Dynamic Page Cache hit.
Stores a response in case of a Dynamic Page Cache miss, if cacheable.
Whether the given response should be cached by Dynamic Page Cache.
Embeds a Response object in a render array so that RenderCache can cache it.
Gets the embedded Response object in a render array.
{@inheritdoc}
Details
__construct(RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, RenderCacheInterface $render_cache, array $renderer_config)
Constructs a new DynamicPageCacheSubscriber object.
onRequest(RequestEvent $event)
Sets a response in case of a Dynamic Page Cache hit.
onResponse(ResponseEvent $event)
Stores a response in case of a Dynamic Page Cache miss, if cacheable.
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.
protected array
responseToRenderArray(CacheableResponseInterface $response)
Embeds a Response object in a render array so that RenderCache can cache it.
protected CacheableResponseInterface
renderArrayToResponse(array $render_array)
Gets the embedded Response object in a render array.
static
getSubscribedEvents()
{@inheritdoc}