class SurrogateKeyGenerator in Fastly 8.3
Generates a 'Surrogate-Key' header in the format expected by Fastly.
Hierarchy
- class \Drupal\fastly\EventSubscriber\SurrogateKeyGenerator implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of SurrogateKeyGenerator
See also
https://docs.fastly.com/guides/purging/getting-started-with-surrogate-keys
1 file declares its use of SurrogateKeyGenerator
1 string reference to 'SurrogateKeyGenerator'
1 service uses SurrogateKeyGenerator
File
- src/
EventSubscriber/ SurrogateKeyGenerator.php, line 16
Namespace
Drupal\fastly\EventSubscriberView source
class SurrogateKeyGenerator implements EventSubscriberInterface {
/**
* The Fastly logger channel.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* CacheTagsHash service.
*
* @var \Drupal\fastly\CacheTagsHash
*/
protected $cacheTagsHash;
/**
* Constructs a new CacheTagsHeaderLimitDetector object.
*
* @param \Psr\Log\LoggerInterface $logger
* The Fastly logger channel.
* @param \Drupal\fastly\CacheTagsHash $cache_tags_hash
* The Fastly logger channel.
*
*/
public function __construct(LoggerInterface $logger, CacheTagsHash $cache_tags_hash) {
$this->logger = $logger;
$this->cacheTagsHash = $cache_tags_hash;
}
/**
* Logs an emergency event when the X-Drupal-Cache-Tags header exceeds 16 KB.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The event to process.
*/
public function onRespond(FilterResponseEvent $event) {
if (!$event
->isMasterRequest()) {
return;
}
$response = $event
->getResponse();
if (method_exists($response, 'getCacheableMetadata')) {
$surrogate_key_header_value = implode(' ', $response
->getCacheableMetadata()
->getCacheTags());
$cache_tags = explode(' ', $surrogate_key_header_value);
$hashes = $this->cacheTagsHash
->cacheTagsToHashes($cache_tags);
$siteId = $this->cacheTagsHash
->getSiteId();
$siteIdHash = $this->cacheTagsHash
->hashInput($siteId);
$hashes[] = $siteIdHash;
$surrogate_key_header_value = implode(' ', $hashes);
$response->headers
->set('Surrogate-Key', $surrogate_key_header_value);
}
}
/**
* Maps cache tags to hashes.
*
* Used when the Surrogate-Key/X-Drupal-Cache-Tags header size otherwise
* exceeds 16 KB.
*
* @deprecated Deprecated and will be removed in future versions. Use
* \Drupal::service('fastly.cache_tags.hash')->cacheTagsToHashes($cache_tags); instead.
*
* @param string[] $cache_tags
* The cache tags in the header.
*
* @return string[]
* The hashes to use instead in the header.
*/
public static function cacheTagsToHashes(array $cache_tags) {
return \Drupal::service('fastly.cache_tags.hash')
->cacheTagsToHashes($cache_tags);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'onRespond',
];
return $events;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SurrogateKeyGenerator:: |
protected | property | CacheTagsHash service. | |
SurrogateKeyGenerator:: |
protected | property | The Fastly logger channel. | |
SurrogateKeyGenerator:: |
public static | function | Maps cache tags to hashes. | |
SurrogateKeyGenerator:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
SurrogateKeyGenerator:: |
public | function | Logs an emergency event when the X-Drupal-Cache-Tags header exceeds 16 KB. | |
SurrogateKeyGenerator:: |
public | function | Constructs a new CacheTagsHeaderLimitDetector object. |