Normal view

Can't set cache age on custom block

I have a weather block I built. Obviously I don't want it cached for the next week, but this is what Drupal is doing using its out of the box page caching.

That would be fine if I could find a way to override this, but I can't. I've tried everything suggested on various forums, but nothing is working.

The last thing I tried was creating a lazy builder but I couldn't even get that to work.

I don't entirely understand how Drupal caching works. But all I want to do is set the cache of the entire node (or ideally just the block) to an hour or two.

I have Redis caching set up if this makes any difference.

Any ideas? Thanks

EDIT:

I seem to have something working, although it's possibly not as granular as I would like. I did 3 things, and I'm not sure which worked and whether it was individually or in combination.

  1. Installed the Cache Control Override mod - https://www.drupal.org/project/cache_control_override

  2. Added this to my settings file:
    $config['system.performance']['cache']['page']['max_age'] = 3600;

  3. Added this to the block build array (in conjunction with existing 'max-age' => 3600,):
    'tags' => ['weather:' . $nid],

Will now do some experimenting to figure out which is essential. Note - It didn't work without 3.

submitted by /u/gwenver
[link] [comments]
❌