class CacheControlEventSubscriber in HTTP Cache Control 8
Same name and namespace in other branches
- 8.2 src/EventSubscriber/CacheControlEventSubscriber.php \Drupal\http_cache_control\EventSubscriber\CacheControlEventSubscriber
Subscriber for adding http cache control headers.
Hierarchy
- class \Drupal\http_cache_control\EventSubscriber\CacheControlEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of CacheControlEventSubscriber
1 string reference to 'CacheControlEventSubscriber'
1 service uses CacheControlEventSubscriber
File
- src/
EventSubscriber/ CacheControlEventSubscriber.php, line 12
Namespace
Drupal\http_cache_control\EventSubscriberView source
class CacheControlEventSubscriber implements EventSubscriberInterface {
/**
* Set http cache control headers.
*/
public function setHeaderCacheControl(FilterResponseEvent $event) {
$config = \Drupal::service('config.factory')
->get('system.performance');
$response = $event
->getResponse();
if (!$response
->isCacheable()) {
return;
}
$max_age = $response
->getMaxAge();
switch ($response
->getStatusCode()) {
case 404:
$ttl = $config
->get('cache.http.404_max_age', $max_age);
break;
case 302:
$ttl = $config
->get('cache.http.302_max_age', $max_age);
break;
case 301:
$ttl = $config
->get('cache.http.301_max_age', $max_age);
break;
default:
$ttl = $config
->get('cache.page.max_age');
break;
}
// Allow modules that set their own max age to retain it.
// If a response max-age is different to the page max-age
// then this suggests the max-age has already been manipulated.
if ($max_age != $config
->get('cache.page.max_age')) {
$ttl = $max_age;
}
$response
->setSharedMaxAge($ttl);
$response
->setClientTtl($config
->get('cache.http.max_age'));
if ($response
->getStatusCode() >= 500) {
$response
->setSharedMaxAge($config
->get('cache.http.5xx_max_age'));
}
elseif ($response
->getStatusCode() < 400) {
// Add stale-if-error directive.
if ($seconds = $config
->get('cache.http.stale_if_error')) {
$response->headers
->addCacheControlDirective('stale-if-error', $seconds);
}
// Add stale-while-revalidate directive.
if ($seconds = $config
->get('cache.http.stale_while_revalidate')) {
$response->headers
->addCacheControlDirective('stale-while-revalidate', $seconds);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Response: set header content for security policy.
$events[KernelEvents::RESPONSE][] = [
'setHeaderCacheControl',
-10,
];
return $events;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheControlEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
CacheControlEventSubscriber:: |
public | function | Set http cache control headers. |