Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Anonymous (not verified) on
Hello,
I've problems with disabling the cache for a block for anonymous users.
I've tried to set max-age
, but this seems only to affect logged in users.
HTTP repsonse headers say:
X-Drupal-Dynamic-Cache:"MISS"
x-drupal-cache:"HIT"
Here's my code:
<?php
namespace Drupal\hello_world\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Hello' Block
*
* @Block(
* id = "hello_block",
* admin_label = @Translation("Hello block"),
* )
*/
class HelloBlock extends BlockBase {
public function build() {
return array(
'#markup' => 'Hello World! ' . time() . '',
'#cache' => array(
'max-age' => 0,
),
);
}
}
Could you please give me some tips how to disable the cache for this block - or how to set it to a lower value (eg. 1 minute)?
In my planned custom block I'll read from another database training dates. So I don't really know when they've changed.
Comments
Issue https://www.drupal.org
Issue https://www.drupal.org/node/2352009 seems to describe my problem.
But I couldn't find a solution up to now.