You are here

class CacheTagsHash in Fastly 8.3

Class CacheTagsHash.

@package Drupal\fastly

Hierarchy

Expanded class hierarchy of CacheTagsHash

3 files declare their use of CacheTagsHash
FastlyCommands.php in src/Commands/FastlyCommands.php
FastlyPurger.php in modules/fastlypurger/src/Plugin/Purge/Purger/FastlyPurger.php
SurrogateKeyGenerator.php in src/EventSubscriber/SurrogateKeyGenerator.php
1 string reference to 'CacheTagsHash'
fastly.services.yml in ./fastly.services.yml
fastly.services.yml
1 service uses CacheTagsHash
fastly.cache_tags.hash in ./fastly.services.yml
Drupal\fastly\CacheTagsHash

File

src/CacheTagsHash.php, line 14

Namespace

Drupal\fastly
View source
class CacheTagsHash implements CacheTagsHashInterface {

  /**
   * ConfigFactory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Fastly settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * CacheTagsHash constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
    $this->config = $config_factory
      ->get('fastly.settings');
  }

  /**
   * Maps cache tags to hashes.
   *
   * Used when the Surrogate-Key/X-Drupal-Cache-Tags header size otherwise
   * exceeds 16 KB.
   *
   * @param string[] $cache_tags
   *   The cache tags in the header.
   *
   * @return string[]
   *   The hashes to use instead in the header.
   */
  public function cacheTagsToHashes(array $cache_tags) {
    $hashes = [];
    $siteId = $this
      ->getSiteId();
    foreach ($cache_tags as $cache_tag) {
      $cache_tag = $siteId ? $siteId . ':' . $cache_tag : $cache_tag;
      $hashes[] = $this
        ->hashInput($cache_tag);
    }
    return $hashes;
  }

  /**
   * Create a hash with the given input and length.
   *
   * @param string $input
   *   The input string to be hashed.
   *
   * @return string
   *   Cryptographic hash with the given length.
   */
  public function hashInput($input) {
    $cache_tags_length = getenv('FASTLY_CACHE_TAG_HASH_LENGTH') ?: $this->config
      ->get('cache_tag_hash_length');
    $cache_tags_length = $cache_tags_length ?: self::CACHE_TAG_HASH_LENGTH;
    return substr(base64_encode(md5($input, true)), 0, $cache_tags_length);
  }

  /**
   * Get site id.
   *
   * @return array|false|mixed|string
   */
  public function getSiteId() {
    $siteId = getenv('FASTLY_SITE_ID') ?: $this->config
      ->get('site_id');
    if (!$siteId) {

      // Create random 8 character string and save it to config.
      $random = new Random();
      $siteId = $random
        ->name();
      $siteId = mb_strtolower($siteId);
      if ($this->configFactory
        ->get('fastly.settings')) {
        $config = $this->configFactory
          ->getEditable('fastly.settings');
        $config
          ->set('site_id', $siteId)
          ->save(TRUE);
      }
    }
    return $siteId;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheTagsHash::$config protected property Fastly settings.
CacheTagsHash::$configFactory protected property ConfigFactory.
CacheTagsHash::cacheTagsToHashes public function Maps cache tags to hashes.
CacheTagsHash::getSiteId public function Get site id.
CacheTagsHash::hashInput public function Create a hash with the given input and length.
CacheTagsHash::__construct public function CacheTagsHash constructor.
CacheTagsHashInterface::CACHE_TAG_HASH_LENGTH constant Default Cache tag hash length.