Issue #2458993 by Wim Leers: #cache[expire] is undocumented, unused, untested: remove it, use #cache[max-age] instead
parent
fd3a981af7
commit
64ac1cb815
|
@ -546,7 +546,7 @@ class Renderer implements RendererInterface {
|
||||||
$data = $this->getCacheableRenderArray($elements);
|
$data = $this->getCacheableRenderArray($elements);
|
||||||
|
|
||||||
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
|
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
|
||||||
$expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : Cache::PERMANENT;
|
$expire = ($elements['#cache']['max-age'] === Cache::PERMANENT) ? Cache::PERMANENT : REQUEST_TIME + $elements['#cache']['max-age'];
|
||||||
$cache = $this->cacheFactory->get($bin);
|
$cache = $this->cacheFactory->get($bin);
|
||||||
|
|
||||||
// Two-tier caching: detect different CID post-bubbling, create redirect,
|
// Two-tier caching: detect different CID post-bubbling, create redirect,
|
||||||
|
|
|
@ -117,10 +117,11 @@ interface RendererInterface {
|
||||||
* - 'contexts': An array of one or more cache context IDs. These are
|
* - 'contexts': An array of one or more cache context IDs. These are
|
||||||
* converted to a final value depending on the request. (e.g. 'user' is
|
* converted to a final value depending on the request. (e.g. 'user' is
|
||||||
* mapped to the current user's ID.)
|
* mapped to the current user's ID.)
|
||||||
|
* - 'max-age': A time in seconds. Zero seconds means it is not cacheable.
|
||||||
|
* \Drupal\Core\Cache\Cache::PERMANENT means it is cacheable forever.
|
||||||
* - 'cid': Specify the cache ID directly. Either 'keys' or 'cid' is
|
* - 'cid': Specify the cache ID directly. Either 'keys' or 'cid' is
|
||||||
* required. If 'cid' is set, 'keys' is ignored. Use only if you have
|
* required. If 'cid' is set, 'keys' is ignored. Use only if you have
|
||||||
* special requirements.
|
* special requirements.
|
||||||
* - 'expire': Set to one of the cache lifetime constants.
|
|
||||||
* - 'bin': Specify a cache bin to cache the element in. Default is
|
* - 'bin': Specify a cache bin to cache the element in. Default is
|
||||||
* 'default'.
|
* 'default'.
|
||||||
* When there is a render cache hit, there is no rendering work left to be
|
* When there is a render cache hit, there is no rendering work left to be
|
||||||
|
|
|
@ -91,11 +91,7 @@ class BlockViewBuilder extends EntityViewBuilder {
|
||||||
'block',
|
'block',
|
||||||
$entity->id(),
|
$entity->id(),
|
||||||
);
|
);
|
||||||
$max_age = $plugin->getCacheMaxAge();
|
$build[$entity_id]['#cache']['keys'] = array_merge($default_cache_keys, $plugin->getCacheKeys());
|
||||||
$build[$entity_id]['#cache'] += array(
|
|
||||||
'keys' => array_merge($default_cache_keys, $plugin->getCacheKeys()),
|
|
||||||
'expire' => ($max_age === Cache::PERMANENT) ? Cache::PERMANENT : REQUEST_TIME + $max_age,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$build[$entity_id] = $this->buildBlock($build[$entity_id]);
|
$build[$entity_id] = $this->buildBlock($build[$entity_id]);
|
||||||
|
|
|
@ -577,6 +577,46 @@ class RendererTest extends RendererTestBase {
|
||||||
$this->assertSame(Cache::mergeTags($expected_tags, ['rendered']), $cache_item->tags);
|
$this->assertSame(Cache::mergeTags($expected_tags, ['rendered']), $cache_item->tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::render
|
||||||
|
* @covers ::doRender
|
||||||
|
* @covers ::cacheGet
|
||||||
|
* @covers ::cacheSet
|
||||||
|
* @covers ::createCacheID
|
||||||
|
*
|
||||||
|
* @dataProvider providerTestRenderCacheMaxAge
|
||||||
|
*/
|
||||||
|
public function testRenderCacheMaxAge($max_age, $is_render_cached, $render_cache_item_expire) {
|
||||||
|
$this->setUpRequest();
|
||||||
|
$this->setupMemoryCache();
|
||||||
|
|
||||||
|
$element = [
|
||||||
|
'#cache' => [
|
||||||
|
'cid' => 'render_cache_test',
|
||||||
|
'max-age' => $max_age,
|
||||||
|
],
|
||||||
|
'#markup' => '',
|
||||||
|
];
|
||||||
|
$this->renderer->render($element);
|
||||||
|
|
||||||
|
$cache_item = $this->cacheFactory->get('render')->get('render_cache_test');
|
||||||
|
if (!$is_render_cached) {
|
||||||
|
$this->assertFalse($cache_item);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->assertNotFalse($cache_item);
|
||||||
|
$this->assertSame($render_cache_item_expire, $cache_item->expire);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function providerTestRenderCacheMaxAge() {
|
||||||
|
return [
|
||||||
|
[0, FALSE, NULL],
|
||||||
|
[60, TRUE, REQUEST_TIME + 60],
|
||||||
|
[Cache::PERMANENT, TRUE, -1],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestAccessClass {
|
class TestAccessClass {
|
||||||
|
|
Loading…
Reference in New Issue