diff --git a/core/core.services.yml b/core/core.services.yml index 2bd8ef6..67b333c 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -75,21 +75,17 @@ services: factory_service: cache_factory arguments: [path] page_cache_request_policy: - class: Drupal\Core\PageCache\RequestPolicy - factory_class: Drupal\Core\PageCache\RequestPolicy - factory_method: createDefault + class: Drupal\Core\PageCache\DefaultRequestPolicy tags: - { name: page_cache_request_policy } page_cache_response_policy: - class: Drupal\Core\PageCache\ResponsePolicy - factory_class: Drupal\Core\PageCache\ResponsePolicy - factory_method: createDefault + class: Drupal\Core\PageCache\DefaultResponsePolicy tags: - { name: page_cache_response_policy } page_cache_kill_switch: class: Drupal\Core\PageCache\ResponsePolicyRule\KillSwitch tags: - - { name: page_cache_response_policy.rule } + - { name: page_cache_response_policy.deny } page_cache_response_subscriber: class: Drupal\Core\PageCache\DefaultCacheControlResponseSubscriber arguments: ['@page_cache_request_policy', '@page_cache_response_policy', '@config.factory', '@settings'] diff --git a/core/lib/Drupal/Core/PageCache/CacheControlResponseSubscriberBase.php b/core/lib/Drupal/Core/PageCache/CacheControlResponseSubscriberBase.php index 2785c01..a2cc207 100644 --- a/core/lib/Drupal/Core/PageCache/CacheControlResponseSubscriberBase.php +++ b/core/lib/Drupal/Core/PageCache/CacheControlResponseSubscriberBase.php @@ -17,7 +17,7 @@ use Symfony\Component\HttpKernel\KernelEvents; /** - * Response subscriber to handle finished responses. + * Base class for subscribers attaching Cache-Control headers to responses. */ abstract class CacheControlResponseSubscriberBase implements EventSubscriberInterface { @@ -45,7 +45,7 @@ protected $maxAge; /** - * Construct Cache-Control header generator publicly cacheable responses. + * Construct base subscriber attaching Cache-Control headers to responses. * * @param \Drupal\Core\PageCache\RequestPolicyRuleInterface $request_policy * A policy rule determining the cacheability of a request. @@ -61,21 +61,21 @@ public function __construct(RequestPolicyRuleInterface $request_policy, Response } /** - * Set the max-age directive to be used when cache-control header is added. + * Set the max-age directive to be used when Cache-Control header is added. */ public function setMaxAge($max_age) { $this->maxAge = $max_age; } /** - * Return the max-age directive to be used when cache-control header is added. + * Return the max-age directive to be used when Cache-Control header is added. */ public function getMaxAge() { return $this->maxAge; } /** - * Determine whether the given request has a custom cache-control header. + * Determine whether the given request has a custom Cache-Control header. * * Upon construction the ResponseHeaderBag is initialized with an empty * Cache-Control header. Consequently it is not possible to check whether the @@ -137,7 +137,7 @@ protected function setNotCacheable(Response $response, Request $request) { * A response object. */ protected function setCacheControlNoCache(Response $response) { - $response->headers->set('Cache-Control', 'no-cache, must-revalidate, post-check=0, pre-check=0'); + $response->headers->set('cache-control', 'no-cache, must-revalidate, post-check=0, pre-check=0'); } /** diff --git a/core/lib/Drupal/Core/PageCache/DefaultCacheControlResponseSubscriber.php b/core/lib/Drupal/Core/PageCache/DefaultCacheControlResponseSubscriber.php index cebb204..11ccbf4 100644 --- a/core/lib/Drupal/Core/PageCache/DefaultCacheControlResponseSubscriber.php +++ b/core/lib/Drupal/Core/PageCache/DefaultCacheControlResponseSubscriber.php @@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\Response; /** - * Response subscriber to handle finished responses. + * Drupal default subscriber attaching Cache-Control headers to responses. */ class DefaultCacheControlResponseSubscriber extends CacheControlResponseSubscriberBase { @@ -25,7 +25,7 @@ class DefaultCacheControlResponseSubscriber extends CacheControlResponseSubscrib protected $omitVaryCookie; /** - * Construct Cache-Control header generator publicly cacheable responses. + * Construct default subscriber attaching Cache-Control headers to responses. * * @param \Drupal\Core\PageCache\RequestPolicyRuleInterface $request_policy * A policy rule determining the cacheability of a request. @@ -61,10 +61,10 @@ protected function setCacheable(Response $response, Request $request) { // are required. $vary_customized = (bool) $response->getVary(); - // Replace the default restrictive cache-control header with a permissive + // Replace the default restrictive Cache-Control header with a permissive // one. $response->headers->set('Cache-Control', 'public'); - $max_age = !$request->cookies->has(session_name()) || $vary_customized ? $this->getMaxAge() : 0; + $max_age = (!$request->cookies->has(session_name()) || $vary_customized) ? $this->getMaxAge() : 0; $response->setMaxAge($max_age); // In order to support HTTP cache-revalidation, ensure that there is a diff --git a/core/lib/Drupal/Core/PageCache/DefaultRequestPolicy.php b/core/lib/Drupal/Core/PageCache/DefaultRequestPolicy.php new file mode 100644 index 0000000..90b0e50 --- /dev/null +++ b/core/lib/Drupal/Core/PageCache/DefaultRequestPolicy.php @@ -0,0 +1,23 @@ +deny(new RequestPolicyRule\CommandLineOrUnsafeMethod()); + $this->allow(new RequestPolicyRule\NoSessionOpen()); + } + +} diff --git a/core/lib/Drupal/Core/PageCache/DefaultResponsePolicy.php b/core/lib/Drupal/Core/PageCache/DefaultResponsePolicy.php new file mode 100644 index 0000000..6686be8 --- /dev/null +++ b/core/lib/Drupal/Core/PageCache/DefaultResponsePolicy.php @@ -0,0 +1,22 @@ +deny(new ResponsePolicyRule\IsBinaryFileResponse()); + } + +} diff --git a/core/lib/Drupal/Core/PageCache/FastPageCache.php b/core/lib/Drupal/Core/PageCache/FastPageCache.php index 4644360..1fda34e 100644 --- a/core/lib/Drupal/Core/PageCache/FastPageCache.php +++ b/core/lib/Drupal/Core/PageCache/FastPageCache.php @@ -13,7 +13,22 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; /** - * A page cache implementation wrapping the drupal kernel. + * A page cache implementation operating before the kernel is booted. + * + * This page cache implementation attempts to deliver cached pages before the + * wrapped Drupal kernel is booted. This allows for better performance compared + * to the post-kernel approach implemented by PageCache (the standard + * implementation). Because neither kernel nor the service container are + * available at cache retrieval time, modifications to settings.php are required + * to set up the fast page cache instance. + * + * Drupal core ships with a storage factory capable of setting up the database + * for operating fast-page cache with the database. Add the following line to + * settings.php to operate the database backed page cache: + * + * @code + * $settings['page_cache_storage'] = 'Drupal\\Core\\PageCache\\Storage\\DatabaseFactory'; + * @endcode */ class FastPageCache extends PageCacheBase { diff --git a/core/lib/Drupal/Core/PageCache/PageCache.php b/core/lib/Drupal/Core/PageCache/PageCache.php index 1166c07..07c12d8 100644 --- a/core/lib/Drupal/Core/PageCache/PageCache.php +++ b/core/lib/Drupal/Core/PageCache/PageCache.php @@ -12,7 +12,13 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; /** - * A page cache implementation wrapping the drupal kernel. + * A page cache implementation operating after the kernel is booted. + * + * This page cache implementation first boots the wrapped Drupal kernel and only + * after that attempts to retrieve a page from the cache. The benefit of this + * approach is that it is possible to configure it entirely from the GUI and + * also alter the cache policy by adding custom request policy rules to the + * container. */ class PageCache extends PageCacheBase { diff --git a/core/lib/Drupal/Core/PageCache/PageCacheBase.php b/core/lib/Drupal/Core/PageCache/PageCacheBase.php index 23a8be5..8879bc2 100644 --- a/core/lib/Drupal/Core/PageCache/PageCacheBase.php +++ b/core/lib/Drupal/Core/PageCache/PageCacheBase.php @@ -113,15 +113,7 @@ protected function deliver(Response $response, Request $request) { * {@inheritdoc} */ public function terminate(Request $request, Response $response) { - // The RequestCloseSubscriber currently breaks on lean requests because it - // unconditionally calls system_run_automated_cron. Therefore make sure that - // the termination-event is only emitted when the response was actually - // handled by the kernel (i.e. the full bootstrap phase was reached). - // - // @see: https://drupal.org/node/2220687 - if ($this->drupalBootstrapGetPhase() == DRUPAL_BOOTSTRAP_FULL) { - $this->kernel->terminate($request, $response); - } + $this->kernel->terminate($request, $response); } /** @@ -131,7 +123,7 @@ public function terminate(Request $request, Response $response) { * The default page request cache policy for the given request. */ protected function getRequestPolicy() { - return RequestPolicy::createDefault(); + return new DefaultRequestPolicy(); } /** diff --git a/core/lib/Drupal/Core/PageCache/RequestPolicy.php b/core/lib/Drupal/Core/PageCache/RequestPolicy.php index 71318a0..c4fa1d0 100644 --- a/core/lib/Drupal/Core/PageCache/RequestPolicy.php +++ b/core/lib/Drupal/Core/PageCache/RequestPolicy.php @@ -77,18 +77,4 @@ public function allow(RequestPolicyRuleInterface $rule) { return $this; } - /** - * Construct and return the default policy. - * - * @return \Drupal\Core\PageCache\RequestPolicy - * The singleton policy instance. - */ - public static function createDefault() { - $policy = new static(); - $policy->deny(new RequestPolicyRule\CommandLineOrUnsafeMethod()); - $policy->allow(new RequestPolicyRule\NoSessionOpen()); - - return $policy; - } - } diff --git a/core/lib/Drupal/Core/PageCache/ResponsePolicy.php b/core/lib/Drupal/Core/PageCache/ResponsePolicy.php index 2c78abe..3bc11ef 100644 --- a/core/lib/Drupal/Core/PageCache/ResponsePolicy.php +++ b/core/lib/Drupal/Core/PageCache/ResponsePolicy.php @@ -49,17 +49,4 @@ public function deny(ResponsePolicyRuleInterface $rule) { return $this; } - /** - * Construct and return the default response policy. - * - * @return \Drupal\Core\PageCache\ResponsePolicy - * The default policy instance. - */ - public static function createDefault() { - $policy = new static(); - $policy->deny(new ResponsePolicyRule\IsBinaryFileResponse()); - - return $policy; - } - } diff --git a/core/lib/Drupal/Core/PageCache/ResponsePolicyRule/IsBinaryFileResponse.php b/core/lib/Drupal/Core/PageCache/ResponsePolicyRule/IsBinaryFileResponse.php index 5087c1a..b928c8c 100644 --- a/core/lib/Drupal/Core/PageCache/ResponsePolicyRule/IsBinaryFileResponse.php +++ b/core/lib/Drupal/Core/PageCache/ResponsePolicyRule/IsBinaryFileResponse.php @@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\Response; /** - * A veto rule evaluating to TRUE when the kill switch was triggered. + * A policy rule evaluating to TRUE when the response is a file download. */ class IsBinaryFileResponse implements ResponsePolicyRuleInterface { diff --git a/core/modules/toolbar/lib/Drupal/toolbar/Controller/ToolbarController.php b/core/modules/toolbar/lib/Drupal/toolbar/Controller/ToolbarController.php index baca823..d89fa75 100644 --- a/core/modules/toolbar/lib/Drupal/toolbar/Controller/ToolbarController.php +++ b/core/modules/toolbar/lib/Drupal/toolbar/Controller/ToolbarController.php @@ -31,7 +31,7 @@ public function subtreesJsonp() { // additional server-side page cache only takes effect when the client // accesses the callback URL again (e.g., after clearing the browser cache // or when force-reloading a Drupal page). - $max_age = 365 * 24 * 60; + $max_age = 365 * 24 * 60 * 60; $response->setPrivate(); $response->setMaxAge($max_age);