You are here

class CacheTagsChecksumMongodb in MongoDB 8

Hierarchy

Expanded class hierarchy of CacheTagsChecksumMongodb

1 string reference to 'CacheTagsChecksumMongodb'
mongodb.services.yml in ./mongodb.services.yml
mongodb.services.yml
1 service uses CacheTagsChecksumMongodb
cache_tags.invalidator.checksum.mongodb in ./mongodb.services.yml
Drupal\mongodb\CacheTagsChecksumMongodb

File

src/CacheTagsChecksumMongodb.php, line 13
Contains \Drupal\mongodb\CacheTagsChecksumMongodb.

Namespace

Drupal\mongodb
View source
class CacheTagsChecksumMongodb implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface {

  /**
   * The mongo collection factory
   *
   * @var \Drupal\mongodb\MongoCollectionFactory
   */
  protected $mongo;

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

  /**
   * 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 = array();

  /**
   * Constructs a DatabaseCacheTagsChecksum object.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(MongoCollectionFactory $mongo) {
    $this->mongo = $mongo;
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateTags(array $tags) {
    foreach ($tags as $tag) {

      // Only invalidate tags once per request unless they are written again.
      if (isset($this->invalidatedTags[$tag])) {
        continue;
      }
      $this->invalidatedTags[$tag] = TRUE;
      unset($this->tagCache[$tag]);
      $this->mongo
        ->get('cachetags')
        ->update([
        'tag' => $tag,
      ], [
        '$inc' => [
          'invalidations' => 1,
        ],
      ], [
        'upsert' => TRUE,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentChecksum(array $tags) {

    // Remove tags that were already invalidated during this request from the
    // static caches so that another invalidation can occur later in the same
    // request. Without that, written cache items would not be invalidated
    // correctly.
    foreach ($tags as $tag) {
      unset($this->invalidatedTags[$tag]);
    }
    return $this
      ->calculateChecksum($tags);
  }

  /**
   * {@inheritdoc}
   */
  public function isValid($checksum, array $tags) {
    return $checksum == $this
      ->calculateChecksum($tags);
  }

  /**
   * {@inheritdoc}
   */
  public function calculateChecksum(array $tags) {
    $checksum = 0;
    $query_tags = array_values(array_diff($tags, array_keys($this->tagCache)));
    if ($query_tags) {
      $db_tags = [];
      $result = $this->mongo
        ->get('cachetags')
        ->find([
        'tag' => [
          '$in' => $query_tags,
        ],
      ]);
      foreach ($result as $tag) {
        $db_tags[$tag['tag']] = $tag['invalidations'];
      }
      $this->tagCache += $db_tags;

      // Fill static cache with empty objects for tags not found in the database.
      $this->tagCache += array_fill_keys(array_diff($query_tags, array_keys($db_tags)), 0);
    }
    foreach ($tags as $tag) {
      $checksum += $this->tagCache[$tag];
    }
    return $checksum;
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {
    $this->tagCache = array();
    $this->invalidatedTags = array();
  }

}

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.
CacheTagsChecksumMongodb::$invalidatedTags protected property A list of tags that have already been invalidated in this request.
CacheTagsChecksumMongodb::$mongo protected property The mongo collection factory
CacheTagsChecksumMongodb::$tagCache protected property Contains already loaded cache invalidations from the database.
CacheTagsChecksumMongodb::calculateChecksum public function
CacheTagsChecksumMongodb::getCurrentChecksum public function Returns the sum total of validations for a given set of tags. Overrides CacheTagsChecksumInterface::getCurrentChecksum
CacheTagsChecksumMongodb::invalidateTags public function Marks cache items with any of the specified tags as invalid. Overrides CacheTagsInvalidatorInterface::invalidateTags
CacheTagsChecksumMongodb::isValid public function Returns whether the checksum is valid for the given cache tags. Overrides CacheTagsChecksumInterface::isValid
CacheTagsChecksumMongodb::reset public function Reset statically cached tags. Overrides CacheTagsChecksumInterface::reset
CacheTagsChecksumMongodb::__construct public function Constructs a DatabaseCacheTagsChecksum object.