You are here

protected function TimestampCacheTagsChecksum::calculateChecksum in Memcache API and Integration 8.2

Calculates the current checksum for a given set of tags.

Parameters

array $tags: The array of tags to calculate the checksum for.

Return value

int The calculated checksum.

2 calls to TimestampCacheTagsChecksum::calculateChecksum()
TimestampCacheTagsChecksum::getCurrentChecksum in src/Cache/TimestampCacheTagsChecksum.php
Returns the sum total of validations for a given set of tags.
TimestampCacheTagsChecksum::isValid in src/Cache/TimestampCacheTagsChecksum.php
Returns whether the checksum is valid for the given cache tags.

File

src/Cache/TimestampCacheTagsChecksum.php, line 101

Class

TimestampCacheTagsChecksum
Cache tags invalidations checksum implementation by timestamp invalidation.

Namespace

Drupal\memcache\Cache

Code

protected function calculateChecksum(array $tags) {
  $query_tags = array_diff($tags, array_keys($this->tagCache));
  if ($query_tags) {
    $backend_tags = $this->invalidator
      ->getLastInvalidationTimestamps($query_tags);
    $this->tagCache += $backend_tags;
    $invalid = array_diff($query_tags, array_keys($backend_tags));
    if (!empty($invalid)) {

      // Invalidate any missing tags now. This is necessary because we cannot
      // zero-optimize our tag list -- we can't tell the difference between
      // a tag that has never been invalidated and a tag that was
      // garbage-collected by the backend!
      //
      // This behavioral difference is the main change that allows us to use
      // an unreliable backend to track cache tag invalidation.
      //
      // Invalidating the tag will cause it to start being tracked, so it can
      // be matched against the checksums stored on items.
      // All items cached after that point with the tag will end up with
      // a valid checksum, and all items cached before that point with the tag
      // will have an invalid checksum, because missing invalidations will
      // keep moving forward in time as they get garbage collected and are
      // re-invalidated.
      //
      // The main effect of all this is that a tag going missing
      // will automatically cause the cache items tagged with it to no longer
      // have the correct checksum.
      foreach ($invalid as $invalid_tag) {
        $this->invalidator
          ->invalidateTimestamp($invalid_tag);
      }
    }
  }

  // The checksum is equal to the *most recent* invalidation of an applicable
  // tag. If the item is untagged, the checksum is always 0.
  return max([
    0,
  ] + array_intersect_key($this->tagCache, array_flip($tags)));
}