返回块的渲染数组并指定以下内容时:
'#cache' => [ 'max-age' => 0 ]
Drupal STILL 缓存阻止!
如何正确禁用它?
答案 0 :(得分:0)
如Drupal 8文档中隐含的here,您必须使与该块关联的标记无效。
我如何最终“禁用”特定块的缓存是使用事件订阅者使所述块的标记无效。
活动订阅者:
class MyBlockCacheInvalidator implements EventSubscriberInterface {
protected $cacheTagsInvalidator;
public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator) {
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
public function onRequest(KernelEvent $event) {
$this->cacheTagsInvalidator->invalidateTags(['my_block_tag']);
}
public static function getSubscribedEvents() {
$events = [];
$events[KernelEvents::REQUEST][] = array('onRequest');
return $events;
}
}
服务条目:
services:
my_block.cache_invalidator:
class: Drupal\my_module\EventSubscriber\MyBlockCacheInvalidator
arguments: ['@cache_tags.invalidator']
tags:
- { name: event_subscriber }
阻止构建响应:
return [
'#theme' => 'my_block_theme',
'#var1' => $some_value,
'#cache' => [
'max-age' => 0,
'tags' => ['my_block_tag']
],
];
这个解决方案对我有用,但效率不高。
答案 1 :(得分:0)
使用值getCacheMaxAge
覆盖块类的0
。
class MyBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
// Returns the block content as a render array
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 0;
}
}