You are here

private function CloudFlarePurger::purgeChunk in CloudFlare 8

Purges a chunk of tags.

Integration point between purge and CloudFlareAPI. Purge requires state tracking on each item purged. This function provides that accounting and calls CloudflareApi.

CloudFlare only allows us to purge 30 tags at once.

Parameters

array $invalidations: Chunk of purge module invalidation objects to purge via CloudFlare.

1 call to CloudFlarePurger::purgeChunk()
CloudFlarePurger::invalidate in modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php
Invalidate content from external caches.

File

modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php, line 165

Class

CloudFlarePurger
CloudFlare purger.

Namespace

Drupal\cloudflarepurger\Plugin\Purge\Purger

Code

private function purgeChunk(array &$invalidations) {

  // This is a unique case where the ApiSdk is being accessed directly and not
  // via a service.  Purging should only ever happen through the purge module
  // which is why this is NOT in a service.
  $api_key = $this->config
    ->get('apikey');
  $email = $this->config
    ->get('email');
  $this->zone = $this->config
    ->get('zone_id');
  $this->zoneApi = new ZoneApi($api_key, $email);
  $api_targets_to_purge = [];

  // This method is unfortunately a bit verbose due to the fact that we
  // need to update the purge states as we proceed.
  foreach ($invalidations as $invalidation) {
    $invalidation
      ->setState(InvalidationInterface::PROCESSING);
    $api_targets_to_purge[] = $invalidation
      ->getExpression();
  }
  if (!$this->areCloudflareComposerDepenciesMet) {
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState(InvalidationInterface::FAILED);
    }
  }
  try {

    // Interface with the CloudFlarePhpSdk.
    $invalidation_type = $invalidations[0]
      ->getPluginId();
    if ($invalidation_type == 'tag') {

      // @todo Remove this wrapper once CloudFlare supports 16k headers.
      // Also invalidate the cache tags as hashes, to automatically also work
      // for responses that exceed CloudFlare's Cache-Tag header limit.
      $hashes = CloudFlareCacheTagHeaderGenerator::cacheTagsToHashes($api_targets_to_purge);
      $this->zoneApi
        ->purgeTags($this->zone, $hashes);
      $this->state
        ->incrementTagPurgeDailyCount();
    }
    elseif ($invalidation_type == 'url') {
      $this->zoneApi
        ->purgeIndividualFiles($this->zone, $api_targets_to_purge);
    }
    elseif ($invalidation_type == 'everything') {
      $this->zoneApi
        ->purgeAllFiles($this->zone);
    }
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState(InvalidationInterface::SUCCEEDED);
    }
  } catch (\Exception $e) {
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState(InvalidationInterface::FAILED);
    }

    // We only want to log a single watchdog error per request. This prevents
    // the log from being flooded.
    $this->logger
      ->error($e
      ->getMessage());
  } finally {
    $this->state
      ->incrementApiRateCount();
  }
}