I'm working on a module, which includes a page that displays a select from a database table. The page content is getting cached for long periods, so is out of date for users.

What's the simplest way to set the cache timeout for this page to a shorter time (or even set that page to never cache)?

(As an aside, I intend to turn this table into an entity and integrate with Views later on, but I need an interim fix).


2 Answers 2


In case you know exactly when something changes (it is your own code doing it), you can also use a cache tag.

'tags' => ['my-tag'] within #cache and then when it changes, you invalidate it with \Drupal\Core\Cache\Cache::invalidateTags(['my-tag']).

The advantage is that your data is cached as long as possible and also immediately updated when it changes.

If you have constant changes, and that tag would actually result in shorter cache times, then setting a max-age might be more efficient, but that's usually not the case.

  • Also with, lets say cron, you can invalidate those tags whenever you want.
    – user21641
    Commented May 4, 2016 at 7:30
public function myPageReponse() {
  return [
    'foo' => [
      '#theme' => 'bar',
      '#cache' => [
        'max-age' => 0 // to completely disable cache for this content

or in route definition:

    no_cache: TRUE
  • That looks like exactly what I want, though I'll probably set the max-age to 60 so the cache will live for a minute. Commented May 3, 2016 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.