Commit 144aa581 authored by alexpott's avatar alexpott

Issue #2461087 by Wim Leers, dawehner: Add 'no_cache' route option to mark a...

Issue #2461087 by Wim Leers, dawehner: Add 'no_cache' route option to mark a route's responses as uncacheable (was: Cron run response should not be cacheable)
parent 2b90b083
...@@ -210,6 +210,12 @@ services: ...@@ -210,6 +210,12 @@ services:
class: Drupal\Core\PageCache\ResponsePolicy\KillSwitch class: Drupal\Core\PageCache\ResponsePolicy\KillSwitch
tags: tags:
- { name: page_cache_response_policy } - { name: page_cache_response_policy }
page_cache_no_cache_routes:
class: Drupal\Core\PageCache\ResponsePolicy\DenyNoCacheRoutes
arguments: ['@current_route_match']
public: false
tags:
- { name: page_cache_response_policy }
page_cache_no_server_error: page_cache_no_server_error:
class: Drupal\Core\PageCache\ResponsePolicy\NoServerError class: Drupal\Core\PageCache\ResponsePolicy\NoServerError
public: false public: false
......
<?php
/**
* @file
* Contains \Drupal\node\PageCache\DenyNoCacheRoutes.
*/
namespace Drupal\Core\PageCache\ResponsePolicy;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Cache policy for routes with the 'no_cache' option set.
*
* This policy rule denies caching of responses generated for routes that have
* the 'no_cache' option set to TRUE.
*/
class DenyNoCacheRoutes implements ResponsePolicyInterface {
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a deny node preview page cache policy.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public function check(Response $response, Request $request) {
if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('no_cache')) {
return static::DENY;
}
}
}
...@@ -49,12 +49,6 @@ function testCronRun() { ...@@ -49,12 +49,6 @@ function testCronRun() {
* need the exact time when cron is triggered. * need the exact time when cron is triggered.
*/ */
function testAutomaticCron() { function testAutomaticCron() {
// Test with a logged in user; anonymous users likely don't cause Drupal to
// fully bootstrap, because of the internal page cache or an external
// reverse proxy. Reuse this user for disabling cron later in the test.
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
$this->drupalLogin($admin_user);
// Ensure cron does not run when the cron threshold is enabled and was // Ensure cron does not run when the cron threshold is enabled and was
// not passed. // not passed.
$cron_last = time(); $cron_last = time();
...@@ -74,6 +68,8 @@ function testAutomaticCron() { ...@@ -74,6 +68,8 @@ function testAutomaticCron() {
$this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron threshold is passed.'); $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron threshold is passed.');
// Disable the cron threshold through the interface. // Disable the cron threshold through the interface.
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
$this->drupalLogin($admin_user);
$this->drupalPostForm('admin/config/system/cron', array('cron_safe_threshold' => 0), t('Save configuration')); $this->drupalPostForm('admin/config/system/cron', array('cron_safe_threshold' => 0), t('Save configuration'));
$this->assertText(t('The configuration options have been saved.')); $this->assertText(t('The configuration options have been saved.'));
$this->drupalLogout(); $this->drupalLogout();
......
...@@ -123,6 +123,8 @@ system.cron: ...@@ -123,6 +123,8 @@ system.cron:
path: '/cron/{key}' path: '/cron/{key}'
defaults: defaults:
_controller: '\Drupal\system\CronController::run' _controller: '\Drupal\system\CronController::run'
options:
no_cache: TRUE
requirements: requirements:
_access_system_cron: 'TRUE' _access_system_cron: 'TRUE'
...@@ -217,6 +219,8 @@ system.run_cron: ...@@ -217,6 +219,8 @@ system.run_cron:
path: '/admin/reports/status/run-cron' path: '/admin/reports/status/run-cron'
defaults: defaults:
_controller: '\Drupal\system\CronController::runManually' _controller: '\Drupal\system\CronController::runManually'
options:
no_cache: TRUE
requirements: requirements:
_permission: 'administer site configuration' _permission: 'administer site configuration'
_csrf_token: 'TRUE' _csrf_token: 'TRUE'
......
...@@ -119,9 +119,6 @@ public function requestDestination(Request $request) { ...@@ -119,9 +119,6 @@ public function requestDestination(Request $request) {
* Try to acquire a named lock and report the outcome. * Try to acquire a named lock and report the outcome.
*/ */
public function lockAcquire() { public function lockAcquire() {
// Don't cache lock-testing pages.
\Drupal::service('page_cache_kill_switch')->trigger();
if ($this->lock->acquire('system_test_lock_acquire')) { if ($this->lock->acquire('system_test_lock_acquire')) {
$this->lock->release('system_test_lock_acquire'); $this->lock->release('system_test_lock_acquire');
return ['#markup' => 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()']; return ['#markup' => 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()'];
...@@ -135,9 +132,6 @@ public function lockAcquire() { ...@@ -135,9 +132,6 @@ public function lockAcquire() {
* Try to acquire a specific lock, and then exit. * Try to acquire a specific lock, and then exit.
*/ */
public function lockExit() { public function lockExit() {
// Don't cache lock-testing pages.
\Drupal::service('page_cache_kill_switch')->trigger();
if ($this->lock->acquire('system_test_lock_exit', 900)) { if ($this->lock->acquire('system_test_lock_exit', 900)) {
echo 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockExit()'; echo 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockExit()';
// The shut-down function should release the lock. // The shut-down function should release the lock.
...@@ -158,9 +152,6 @@ public function lockExit() { ...@@ -158,9 +152,6 @@ public function lockExit() {
* The text to display. * The text to display.
*/ */
public function lockPersist($lock_name) { public function lockPersist($lock_name) {
// Don't cache lock-testing pages.
\Drupal::service('page_cache_kill_switch')->trigger();
if ($this->persistentLock->acquire($lock_name)) { if ($this->persistentLock->acquire($lock_name)) {
return ['#markup' => 'TRUE: Lock successfully acquired in SystemTestController::lockPersist()']; return ['#markup' => 'TRUE: Lock successfully acquired in SystemTestController::lockPersist()'];
} }
......
...@@ -34,6 +34,8 @@ system_test.lock_acquire: ...@@ -34,6 +34,8 @@ system_test.lock_acquire:
defaults: defaults:
_title: 'Lock acquire' _title: 'Lock acquire'
_controller: '\Drupal\system_test\Controller\SystemTestController::lockAcquire' _controller: '\Drupal\system_test\Controller\SystemTestController::lockAcquire'
options:
no_cache: TRUE
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
...@@ -42,6 +44,8 @@ system_test.lock_exit: ...@@ -42,6 +44,8 @@ system_test.lock_exit:
defaults: defaults:
_title: 'Lock acquire then exit' _title: 'Lock acquire then exit'
_controller: '\Drupal\system_test\Controller\SystemTestController::lockExit' _controller: '\Drupal\system_test\Controller\SystemTestController::lockExit'
options:
no_cache: TRUE
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
...@@ -50,6 +54,8 @@ system_test.lock_persist: ...@@ -50,6 +54,8 @@ system_test.lock_persist:
defaults: defaults:
_title: 'Persistent lock acquire' _title: 'Persistent lock acquire'
_controller: '\Drupal\system_test\Controller\SystemTestController::lockPersist' _controller: '\Drupal\system_test\Controller\SystemTestController::lockPersist'
options:
no_cache: TRUE
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Drupal\user\UserDataInterface; use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
use Drupal\user\UserStorageInterface; use Drupal\user\UserStorageInterface;
...@@ -43,13 +42,6 @@ class UserController extends ControllerBase { ...@@ -43,13 +42,6 @@ class UserController extends ControllerBase {
*/ */
protected $userData; protected $userData;
/**
* The page cache killswitch.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $pageCacheKillSwitch;
/** /**
* Constructs a UserController object. * Constructs a UserController object.
* *
...@@ -59,14 +51,11 @@ class UserController extends ControllerBase { ...@@ -59,14 +51,11 @@ class UserController extends ControllerBase {
* The user storage. * The user storage.
* @param \Drupal\user\UserDataInterface $user_data * @param \Drupal\user\UserDataInterface $user_data
* The user data service. * The user data service.
* @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $page_cache_kill_switch
* The page cache killswitch.
*/ */
public function __construct(DateFormatter $date_formatter, UserStorageInterface $user_storage, UserDataInterface $user_data, KillSwitch $page_cache_kill_switch) { public function __construct(DateFormatter $date_formatter, UserStorageInterface $user_storage, UserDataInterface $user_data) {
$this->dateFormatter = $date_formatter; $this->dateFormatter = $date_formatter;
$this->userStorage = $user_storage; $this->userStorage = $user_storage;
$this->userData = $user_data; $this->userData = $user_data;
$this->pageCacheKillSwitch = $page_cache_kill_switch;
} }
/** /**
...@@ -76,8 +65,7 @@ public static function create(ContainerInterface $container) { ...@@ -76,8 +65,7 @@ public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('date.formatter'), $container->get('date.formatter'),
$container->get('entity.manager')->getStorage('user'), $container->get('entity.manager')->getStorage('user'),
$container->get('user.data'), $container->get('user.data')
$container->get('page_cache_kill_switch')
); );
} }
...@@ -98,9 +86,6 @@ public static function create(ContainerInterface $container) { ...@@ -98,9 +86,6 @@ public static function create(ContainerInterface $container) {
* If the login link is for a blocked user or invalid user ID. * If the login link is for a blocked user or invalid user ID.
*/ */
public function resetPass($uid, $timestamp, $hash) { public function resetPass($uid, $timestamp, $hash) {
// Don't cache the password reset page.
$this->pageCacheKillSwitch->trigger();
$account = $this->currentUser(); $account = $this->currentUser();
$config = $this->config('user.settings'); $config = $this->config('user.settings');
// When processing the one-time login link, we have to make sure that a user // When processing the one-time login link, we have to make sure that a user
......
...@@ -148,3 +148,4 @@ user.reset: ...@@ -148,3 +148,4 @@ user.reset:
_access: 'TRUE' _access: 'TRUE'
options: options:
_maintenance_access: TRUE _maintenance_access: TRUE
no_cache: TRUE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment