You are here

class RedisCacheTagsChecksum in Redis 8

Cache tags invalidations checksum implementation that uses redis.

Hierarchy

Expanded class hierarchy of RedisCacheTagsChecksum

1 string reference to 'RedisCacheTagsChecksum'
example.services.yml in ./example.services.yml
example.services.yml
1 service uses RedisCacheTagsChecksum
cache_tags.invalidator.checksum in ./example.services.yml
Drupal\redis\Cache\RedisCacheTagsChecksum

File

src/Cache/RedisCacheTagsChecksum.php, line 14

Namespace

Drupal\redis\Cache
View source
class RedisCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface {
  use RedisPrefixTrait;
  use CacheTagsChecksumTrait;

  /**
   * Contains already loaded cache invalidations from the database.
   *
   * @var array
   */
  protected $tagCache = [];

  /**
   * A list of tags that have already been invalidated in this request.
   *
   * Used to prevent the invalidation of the same cache tag multiple times.
   *
   * @var array
   */
  protected $invalidatedTags = [];

  /**
   * {@inheritdoc}
   */
  protected $client;

  /**
   * @var string
   */
  protected $clientType;

  /**
   * Creates a PHpRedis cache backend.
   */
  public function __construct(ClientFactory $factory) {
    $this->client = $factory
      ->getClient();
    $this->clientType = $factory
      ->getClientName();
  }

  /**
   * {@inheritdoc}
   */
  public function doInvalidateTags(array $tags) {
    $keys = array_map([
      $this,
      'getTagKey',
    ], $tags);

    // We want to differentiate between PhpRedis and Redis clients.
    if ($this->clientType === 'PhpRedis') {
      $multi = $this->client
        ->multi();
      foreach ($keys as $key) {
        $multi
          ->incr($key);
      }
      $multi
        ->exec();
    }
    elseif ($this->clientType === 'Predis') {
      $pipe = $this->client
        ->pipeline();
      foreach ($keys as $key) {
        $pipe
          ->incr($key);
      }
      $pipe
        ->execute();
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getTagInvalidationCounts(array $tags) {
    $keys = array_map([
      $this,
      'getTagKey',
    ], $tags);

    // The mget command returns the values as an array with numeric keys,
    // combine it with the tags array to get the expected return value and run
    // it through intval() to convert to integers and FALSE to 0.
    $values = $this->client
      ->mget($keys);
    return $values ? array_map('intval', array_combine($tags, $values)) : [];
  }

  /**
   * Return the key for the given cache tag.
   *
   * @param string $tag
   *   The cache tag.
   *
   * @return string
   *   The prefixed cache tag.
   */
  protected function getTagKey($tag) {
    return $this
      ->getPrefix() . ':cachetags:' . $tag;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDatabaseConnection() {

    // This is not injected to avoid a dependency on the database in the
    // critical path. It is only needed during cache tag invalidations.
    return \Drupal::database();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheTagsChecksumInterface::INVALID_CHECKSUM_WHILE_IN_TRANSACTION constant The invalid checksum returned if a database transaction is in progress.
CacheTagsChecksumTrait::$delayedTags protected property The set of cache tags whose invalidation is delayed.
CacheTagsChecksumTrait::calculateChecksum protected function Calculates the current checksum for a given set of tags.
CacheTagsChecksumTrait::getCurrentChecksum public function Implements \Drupal\Core\Cache\CacheTagsChecksumInterface::getCurrentChecksum()
CacheTagsChecksumTrait::invalidateTags public function Implements \Drupal\Core\Cache\CacheTagsChecksumInterface::invalidateTags()
CacheTagsChecksumTrait::isValid public function Implements \Drupal\Core\Cache\CacheTagsChecksumInterface::isValid()
CacheTagsChecksumTrait::reset public function Implements \Drupal\Core\Cache\CacheTagsChecksumInterface::reset()
CacheTagsChecksumTrait::rootTransactionEndCallback public function Callback to be invoked just after a database transaction gets committed.
RedisCacheTagsChecksum::$client protected property
RedisCacheTagsChecksum::$clientType protected property
RedisCacheTagsChecksum::$invalidatedTags protected property A list of tags that have already been invalidated in this request. Overrides CacheTagsChecksumTrait::$invalidatedTags
RedisCacheTagsChecksum::$tagCache protected property Contains already loaded cache invalidations from the database. Overrides CacheTagsChecksumTrait::$tagCache
RedisCacheTagsChecksum::doInvalidateTags public function Marks cache items with any of the specified tags as invalid. Overrides CacheTagsChecksumTrait::doInvalidateTags
RedisCacheTagsChecksum::getDatabaseConnection protected function Returns the database connection. Overrides CacheTagsChecksumTrait::getDatabaseConnection
RedisCacheTagsChecksum::getTagInvalidationCounts protected function Fetches invalidation counts for cache tags. Overrides CacheTagsChecksumTrait::getTagInvalidationCounts
RedisCacheTagsChecksum::getTagKey protected function Return the key for the given cache tag.
RedisCacheTagsChecksum::__construct public function Creates a PHpRedis cache backend.
RedisPrefixTrait::$prefix protected property
RedisPrefixTrait::getDefaultPrefix protected function Get global default prefix
RedisPrefixTrait::getPrefix protected function Get prefix
RedisPrefixTrait::setPrefix public function Set prefix