You are here

class AkamaiTagPurger in Akamai 8.3

Akamai Tag Purger.

Plugin annotation


@PurgePurger(
  id = "akamai_tag",
  label = @Translation("Akamai Tag Purger"),
  description = @Translation("Provides a Purge service for Akamai Fast Purge Cache Tags."),
  types = {"tag"},
  configform = "Drupal\akamai\Form\ConfigForm",
)

Hierarchy

Expanded class hierarchy of AkamaiTagPurger

1 file declares its use of AkamaiTagPurger
AkamaiTagPurgerTest.php in tests/src/Unit/Plugin/Purge/Purger/AkamaiTagPurgerTest.php

File

src/Plugin/Purge/Purger/AkamaiTagPurger.php, line 25

Namespace

Drupal\akamai\Plugin\Purge\Purger
View source
class AkamaiTagPurger extends PurgerBase {

  /**
   * Web services client for Akamai API.
   *
   * @var \Drupal\akamai\AkamaiClient
   */
  protected $client;

  /**
   * Akamai client config.
   *
   * @var \Drupal\Core\Config
   */
  protected $akamaiClientConfig;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * A logger instance.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('event_dispatcher'), $container
      ->get('akamai.client.factory'), $container
      ->get('logger.channel.akamai'));
  }

  /**
   * Constructs a \Drupal\Component\Plugin\AkamaiPurger.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The factory for configuration objects.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\akamai\AkamaiClientFactory $akamai_client_factory
   *   The akamai client factory.
   * @param \Psr\Log\LoggerInterface $logger
   *   Logger interface.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config, EventDispatcherInterface $event_dispatcher, AkamaiClientFactory $akamai_client_factory, LoggerInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->client = $akamai_client_factory
      ->get();
    $this->akamaiClientConfig = $config
      ->get('akamai.settings');
    $this->eventDispatcher = $event_dispatcher;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function getTimeHint() {
    $timeout = (double) $this->akamaiClientConfig
      ->get('timeout');

    // The max value for getTimeHint is 10.00.
    if ($timeout > 10) {
      return 10;
    }
    elseif ($timeout > 0) {
      return $timeout;
    }
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  public function invalidate(array $invalidations) {

    // Build array of tag strings.
    $tags_to_clear = [];

    // Get Cache Tag formatter.
    $formatter = \Drupal::service('akamai.helper.cachetagformatter');
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState(InvalidationInterface::PROCESSING);
      $tag = $formatter
        ->format($invalidation
        ->getExpression());
      if (mb_strlen($tag) > 128) {
        $this->logger
          ->warning('Cache Tag %tag has exceeded the Akamai 128 character tag maximum length.', [
          '%tag' => $tag,
        ]);
      }

      // Remove duplicate entries.
      $tags_to_clear[$tag] = $tag;
    }

    // Change it to a normal array so the JSON conversion goes as expected.
    $tags_to_clear = array_values($tags_to_clear);

    // Set invalidation type to tag.
    $this->client
      ->setType('tag');

    // Instantiate event and alter tags with subscribers.
    $event = new AkamaiPurgeEvents($tags_to_clear);
    $this->eventDispatcher
      ->dispatch(AkamaiPurgeEvents::PURGE_CREATION, $event);
    $tags_to_clear = $event->data;

    // Purge tags.
    $invalidation_state = InvalidationInterface::SUCCEEDED;
    $result = $this->client
      ->purgeTags($tags_to_clear);
    if (!$result) {
      $invalidation_state = InvalidationInterface::FAILED;
    }

    // Set Invalidation status.
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState($invalidation_state);
    }
  }

  /**
   * Use a static value for purge queuer performance.
   *
   * @see parent::hasRunTimeMeasurement()
   */
  public function hasRuntimeMeasurement() {
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AkamaiTagPurger::$akamaiClientConfig protected property Akamai client config.
AkamaiTagPurger::$client protected property Web services client for Akamai API.
AkamaiTagPurger::$eventDispatcher protected property The event dispatcher.
AkamaiTagPurger::$logger protected property A logger instance. Overrides PurgeLoggerAwareTrait::$logger
AkamaiTagPurger::create public static function Creates an instance of the plugin. Overrides PurgerBase::create
AkamaiTagPurger::getTimeHint public function Get the maximum number of seconds, processing a single invalidation takes. Overrides PurgerBase::getTimeHint
AkamaiTagPurger::hasRuntimeMeasurement public function Use a static value for purge queuer performance. Overrides PurgerCapacityDataInterface::hasRuntimeMeasurement
AkamaiTagPurger::invalidate public function Invalidate content from external caches. Overrides PurgerInterface::invalidate
AkamaiTagPurger::__construct public function Constructs a \Drupal\Component\Plugin\AkamaiPurger. Overrides PurgerBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PurgeLoggerAwareTrait::logger public function
PurgerBase::$id protected property Unique instance ID for this purger.
PurgerBase::$runtimeMeasurement protected property The runtime measurement counter.
PurgerBase::delete public function The current instance of this purger plugin is about to be deleted. Overrides PurgerInterface::delete 1
PurgerBase::getCooldownTime public function Get the time in seconds to wait after invalidation. Overrides PurgerCapacityDataInterface::getCooldownTime
PurgerBase::getId public function Retrieve the unique instance ID for this purger instance. Overrides PurgerInterface::getId
PurgerBase::getIdealConditionsLimit public function Get the maximum number of invalidations that this purger can process. Overrides PurgerCapacityDataInterface::getIdealConditionsLimit 1
PurgerBase::getLabel public function Retrieve the user-readable label for this purger instance. Overrides PurgerInterface::getLabel
PurgerBase::getRuntimeMeasurement public function Get the runtime measurement counter. Overrides PurgerCapacityDataInterface::getRuntimeMeasurement
PurgerBase::getTypes public function Retrieve the list of supported invalidation types. Overrides PurgerInterface::getTypes
PurgerBase::routeTypeToMethod public function Route certain type of invalidations to other methods. Overrides PurgerInterface::routeTypeToMethod
PurgerBase::setRuntimeMeasurement public function Inject the runtime measurement counter. Overrides PurgerCapacityDataInterface::setRuntimeMeasurement
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.