Commit fa326794 authored by catch's avatar catch

Issue #2094241 by moshe weitzmann, amateescu, Wim Leers: Cache tag the page cache.

parent 90f47738
......@@ -3182,7 +3182,7 @@ function drupal_page_set_cache(Response $response, Request $request) {
// because by the time it is read, the configuration might change.
'page_compressed' => $page_compressed,
),
'tags' => array('content' => TRUE),
'tags' => array('content' => TRUE) + drupal_cache_tags_page_get(),
'expire' => CacheBackendInterface::CACHE_PERMANENT,
'created' => REQUEST_TIME,
);
......@@ -4218,6 +4218,38 @@ function drupal_render_collect_cache_tags($element, $tags = array()) {
return $tags;
}
/**
* A #post_render callback at the top level of the $page array. Collects the
* tags for use in page cache.
*
* @param string $children
* An HTML string of rendered output.
* @param array $elements
* A render array.
*
* @return string
* The same $children that was passed in - no modifications.
*/
function drupal_post_render_cache_tags_page_set($children, array $elements) {
if (drupal_page_is_cacheable()) {
$tags = &drupal_static('system_cache_tags_page', array());
$tags = drupal_render_collect_cache_tags($elements);
}
return $children;
}
/**
* Return the cache tags that were stored during drupal_render_page().
*
* @return array
* An array of cache tags.
*
* @see drupal_post_render_cache_tags_page_set()
*/
function drupal_cache_tags_page_get() {
return drupal_static('system_cache_tags_page', array());
}
/**
* Prepares an element for caching based on a query.
*
......
......@@ -88,7 +88,7 @@ protected function alterBuild(array &$build, EntityInterface $entity, EntityDisp
// The node 'submitted' info is not rendered in a standard way (renderable
// array) so we have to add a cache tag manually.
$build['#cache']['tags']['user'][] = $entity->uid;
$build['#cache']['tags']['user'][] = $entity->getAuthorId();
}
}
......@@ -9,6 +9,7 @@
use Symfony\Component\Routing\RequestContext;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Cache\Cache;
/**
* Enables the page cache and tests it with various HTTP requests.
......@@ -41,6 +42,29 @@ function setUp() {
->save();
}
/**
* Test that cache tags are properly persisted.
*
* Since tag based invalidation works, we know that our tag properly
* persisted.
*/
function testPageCacheTags() {
$config = \Drupal::config('system.performance');
$config->set('cache.page.use_internal', 1);
$config->set('cache.page.max_age', 300);
$config->save();
$path = 'system-test/cache_tags_page';
$tags = array('system_test_cache_tags_page' => TRUE);
$this->drupalGet($path);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->drupalGet($path);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
Cache::invalidateTags($tags);
$this->drupalGet($path);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
}
/**
* Tests support for different cache items with different Accept headers.
*/
......
......@@ -261,6 +261,7 @@ function system_element_info() {
'#theme_wrappers' => array('form'),
);
$types['page'] = array(
'#post_render' => array('drupal_post_render_cache_tags_page_set'),
'#show_messages' => TRUE,
'#theme' => 'page',
'#theme_wrappers' => array('html'),
......
......@@ -38,6 +38,17 @@ public function lockExit() {
return system_test_lock_exit();
}
/**
* Set cache tag on on the returned render array.
*/
function system_test_cache_tags_page() {
$build['main'] = array(
'#markup' => 'Cache tags page example',
'#cache' => array('tags' => array('system_test_cache_tags_page' => TRUE)),
);
return $build;
}
/**
* @todo Remove system_test_authorize_init_page().
*/
......
......@@ -45,6 +45,13 @@ system_test.lock_exit:
requirements:
_access: 'TRUE'
system_test.cache_tags_page:
path: '/system-test/cache_tags_page'
defaults:
_controller: '\Drupal\system_test\Controller\SystemTestController::system_test_cache_tags_page'
requirements:
_access: 'TRUE'
system_test.authorize_init:
path: '/system-test/authorize-init/{page_title}'
defaults:
......
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