You are here

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
CacheTagsInvalidator.php in src/CacheTagsInvalidator.php
1 string reference to 'SurrogateKeyGenerator'
fastly.services.yml in ./fastly.services.yml
fastly.services.yml
1 service uses SurrogateKeyGenerator
fastly.cache_tags.surrogate_key_generator in ./fastly.services.yml
Drupal\fastly\EventSubscriber\SurrogateKeyGenerator

File

src/EventSubscriber/SurrogateKeyGenerator.php, line 16

Namespace

Drupal\fastly\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
SurrogateKeyGenerator::$cacheTagsHash protected property CacheTagsHash service.
SurrogateKeyGenerator::$logger protected property The Fastly logger channel.
SurrogateKeyGenerator::cacheTagsToHashes Deprecated public static function Maps cache tags to hashes.
SurrogateKeyGenerator::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SurrogateKeyGenerator::onRespond public function Logs an emergency event when the X-Drupal-Cache-Tags header exceeds 16 KB.
SurrogateKeyGenerator::__construct public function Constructs a new CacheTagsHeaderLimitDetector object.