| name | drupal-cache-maxage |
| description | Drupal cache max-age configuration and behavior. Use when asked about time-based cache expiration, Cache::PERMANENT, max-age 0 issues, why Page Cache ignores max-age, or when content appears stale despite time expiration. Critical for understanding caching layer differences. |
Drupal Cache Max-Age
Max-age defines time-based cache validity in seconds.
When to Use
- Caching external API data with known refresh intervals
- Time-sensitive content (countdowns, live scores)
- Understanding why anonymous pages stay cached
- Debugging UNCACHEABLE responses
Critical Behavior
Page Cache ignores max-age - Only uses tags for invalidation.
Dynamic Page Cache respects max-age - max-age:0 causes UNCACHEABLE.
Max-age bubbles up - Minimum value wins across render tree.
Max-Age Values
| Value | Meaning |
|---|
0 | Uncacheable - DANGER: bubbles up! |
Cache::PERMANENT (-1) | Forever until tag invalidation |
3600 | 1 hour |
86400 | 1 day |
Examples
Example 1: External API Data
Input: "I fetch weather data from an API, how do I cache it for 15 minutes?"
Output:
$build = [
'#markup' => $this->weatherService->getCurrentWeather(),
'#cache' => [
'max-age' => 900,
'tags' => ['weather:current'],
],
];
Example 2: Cache Forever Until Invalidation
Input: "My content only changes when the entity is edited"
Output:
use Drupal\Core\Cache\Cache;
$build = [
'#markup' => $node->getTitle(),
'#cache' => [
'max-age' => Cache::PERMANENT,
'tags' => $node->getCacheTags(),
],
];
Example 3: Time-Sensitive Countdown
Input: "I show a countdown to an event, how do I handle cache?"
Output:
public function buildCountdown(): array {
$event_time = strtotime('2024-12-31 23:59:59');
$remaining = $event_time - time();
return [
'#markup' => $this->formatCountdown($remaining),
'#cache' => [
'max-age' => $remaining > 0 ? min($remaining, 60) : 0,
],
];
}
Example 4: max-age:0 Causes Page UNCACHEABLE
Input: "Why does X-Drupal-Dynamic-Cache show UNCACHEABLE?"
Output:
$build['timestamp'] = [
'#markup' => time(),
'#cache' => ['max-age' => 0],
];
$build['timestamp'] = [
'#lazy_builder' => ['my_module.lazy:renderTimestamp', []],
'#create_placeholder' => TRUE,
];
Example 5: Page Cache Ignores max-age
Input: "I set max-age to 300 but anonymous users still see old content after 5 minutes"
Output:
Cache::invalidateTags(['my_module:data']);
my_module.dynamic_page:
path: '/dynamic'
defaults:
_controller: '\Drupal\my_module\Controller::page'
options:
no_cache: TRUE
\Drupal::service('page_cache_kill_switch')->trigger();
Example 6: Finding max-age:0 Source
Input: "How do I find what's setting max-age to 0?"
Output:
if ($a === 0 || $b === 0) {
\Drupal::logger('cache_debug')->warning(
'max-age:0 at: @trace',
['@trace' => json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5))]
);
}
Example 7: Browser Cache-Control Headers
Input: "How do I control browser caching separately from Drupal cache?"
Output:
use Symfony\Component\HttpFoundation\Response;
public function myPage(): Response {
$build = ['#markup' => 'Content'];
$html = \Drupal::service('renderer')->renderRoot($build);
$response = new Response($html);
$response->headers->set('Cache-Control', 'public, max-age=3600');
return $response;
}
Max-Age Bubbling Behavior
$build = [
'#markup' => 'Parent',
'#cache' => ['max-age' => 3600],
];
$build['child'] = [
'#markup' => 'Child',
'#cache' => ['max-age' => 0],
];
Common Mistakes
| Mistake | Impact | Solution |
|---|
| max-age:0 in render array | Entire page uncacheable | Use lazy builder |
| Relying on max-age for Page Cache | Pages never expire | Use cache tags + invalidation |
| Short max-age on stable content | Unnecessary re-renders | Use tags, set PERMANENT |
| Forgetting bubbling | Child max-age:0 breaks parent | Audit all render elements |
Debugging
curl -sI https://site.com/ | grep -i 'cache-control\|x-drupal-cache-max-age'
drush cache:clear render
curl -sI https://site.com/node/1