.../ProxyClass/SmartCache/DefaultRequestPolicy.php | 92 ---------------------- .../src/EventSubscriber/SmartCacheSubscriber.php | 38 ++++++--- 2 files changed, 27 insertions(+), 103 deletions(-) diff --git a/core/lib/Drupal/Core/ProxyClass/SmartCache/DefaultRequestPolicy.php b/core/lib/Drupal/Core/ProxyClass/SmartCache/DefaultRequestPolicy.php deleted file mode 100644 index 2fdebb5..0000000 --- a/core/lib/Drupal/Core/ProxyClass/SmartCache/DefaultRequestPolicy.php +++ /dev/null @@ -1,92 +0,0 @@ -container = $container; - $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id; - } - - /** - * Lazy loads the real service from the container. - * - * @return object - * Returns the constructed real service. - */ - protected function lazyLoadItself() - { - if (!isset($this->service)) { - $this->service = $this->container->get($this->drupalProxyOriginalServiceId); - } - - return $this->service; - } - - /** - * {@inheritdoc} - */ - public function check(\Symfony\Component\HttpFoundation\Request $request) - { - return $this->lazyLoadItself()->check($request); - } - - /** - * {@inheritdoc} - */ - public function addPolicy(\Drupal\Core\PageCache\RequestPolicyInterface $policy) - { - return $this->lazyLoadItself()->addPolicy($policy); - } - - } - -} diff --git a/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php b/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php index 2e58521..9f9fe33 100644 --- a/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php +++ b/core/modules/smart_cache/src/EventSubscriber/SmartCacheSubscriber.php @@ -46,7 +46,23 @@ class SmartCacheSubscriber implements EventSubscriberInterface { * @see onRouteMatch() * @see onRespond() */ - const SMART_CACHE_REQUEST_POLICY_RESULT = '_smart_cache_request_policy_result'; + const ATTRIBUTE_REQUEST_POLICY_RESULT = '_smart_cache_request_policy_result'; + + /** + * Attribute name of the Smart Cache cache contexts for the current route CID. + * + * @see onRouteMatch() + * @see onRespond() + */ + const ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE_CID = '_smart_cache_cache_contexts_for_route_cid'; + + /** + * Attribute name of the Smart Cache cache contexts for the current route. + * + * @see onRouteMatch() + * @see onRespond() + */ + const ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE = '_smart_cache_cache_contexts_for_route'; /** * The cache contexts manager. @@ -117,7 +133,7 @@ public function onRouteMatch(GetResponseEvent $event) { // not have to redo the request policy check. $request = $event->getRequest(); $request_policy_result = $this->requestPolicy->check($request); - $event->getRequest()->attributes->set(self::SMART_CACHE_REQUEST_POLICY_RESULT, $request_policy_result); + $request->attributes->set(self::ATTRIBUTE_REQUEST_POLICY_RESULT, $request_policy_result); if ($request_policy_result === RequestPolicyInterface::DENY) { return; } @@ -125,6 +141,8 @@ public function onRouteMatch(GetResponseEvent $event) { // Get the contexts by which the current route's response must be varied. $cid = implode(':', $this->cacheContextsManager->convertTokensToKeys(['route'])->getKeys()); $cache_contexts_for_route = $this->smartContextsCache->get($cid); + $request->attributes->set(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE_CID, $cid); + $request->attributes->set(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE, $cache_contexts_for_route); // Get & set the cached HTML response for the current route, if any. if ($cache_contexts_for_route !== FALSE) { @@ -164,17 +182,10 @@ public function onResponse(FilterResponseEvent $event) { // policies are not met. // @see onRouteMatch() $request = $event->getRequest(); - if ($request->attributes->get(self::SMART_CACHE_REQUEST_POLICY_RESULT) === RequestPolicyInterface::DENY || $this->responsePolicy->check($response, $request) === ResponsePolicyInterface::DENY) { + if ($request->attributes->get(self::ATTRIBUTE_REQUEST_POLICY_RESULT) === RequestPolicyInterface::DENY || $this->responsePolicy->check($response, $request) === ResponsePolicyInterface::DENY) { return; } - // Get the contexts by which the current route's response must be varied. - $contexts_cid = $this->cacheContextsManager->convertTokensToKeys(['route'])->getKeys()[0]; - $stored_cache_contexts = $this->smartContextsCache->get($contexts_cid); - if ($stored_cache_contexts !== FALSE) { - $stored_cache_contexts = $stored_cache_contexts->data; - } - // Get the cacheability metadata. $html_cacheability = CacheableMetadata::createFromObject($response->getCacheableMetadata()) ->addCacheContexts(['route']) @@ -190,6 +201,11 @@ public function onResponse(FilterResponseEvent $event) { // SmartCache only caches cacheable HTML responses, i.e. with a max-age > 0. if ($html_cacheability->getCacheMaxAge() !== 0) { $this->normalizeCacheability($html_cacheability); + + // Get the contexts by which the current route's response must be varied. + $cache_contexts_for_route = $request->attributes->get(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE); + $stored_cache_contexts = ($cache_contexts_for_route !== FALSE) ? $cache_contexts_for_route->data : NULL; + // If the set of cache contexts is different, store the union of the // already stored cache contexts and the contexts for this request. if ($html_cacheability->getCacheContexts() !== $stored_cache_contexts) { @@ -197,7 +213,7 @@ public function onResponse(FilterResponseEvent $event) { $html_cacheability->addCacheContexts($stored_cache_contexts); $this->normalizeCacheability($html_cacheability); } - $this->smartContextsCache->set($contexts_cid, $html_cacheability->getCacheContexts()); + $this->smartContextsCache->set($request->attributes->get(self::ATTRIBUTE_CACHE_CONTEXTS_FOR_ROUTE_CID), $html_cacheability->getCacheContexts()); } // Finally, cache the HTML response by those contexts.