Verified Commit 9bc3874f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3423272 by catch, kristiaanvandeneynde: Return early from more cache tag operations

(cherry picked from commit 08bf243d)
parent 86594c44
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ public function rootTransactionEndCallback($success) {
   * Implements \Drupal\Core\Cache\CacheTagsInvalidatorInterface::invalidateTags()
   */
  public function invalidateTags(array $tags) {
    // Only invalidate tags once per request unless they are written again.
    foreach ($tags as $key => $tag) {
      if (isset($this->invalidatedTags[$tag])) {
        unset($tags[$key]);
@@ -139,6 +138,11 @@ public function isValid($checksum, array $tags) {
   */
  protected function calculateChecksum(array $tags) {
    $checksum = 0;
    // If there are no cache tags, then there is no cache tag to checksum,
    // so return early..
    if (empty($tags)) {
      return $checksum;
    }

    $query_tags = array_diff($tags, array_keys($this->tagCache));
    if ($query_tags) {
+11 −0
Original line number Diff line number Diff line
@@ -19,6 +19,12 @@ public function __construct(protected readonly CacheTagsChecksumInterface $check
   * {@inheritdoc}
   */
  public function getCurrentChecksum(array $tags) {
    // If there are no cache tags, there is no checksum to get and the decorated
    // method will be a no-op, so don't log anything.
    if (empty($tags)) {
      return $this->checksumInvalidator->getCurrentChecksum($tags);
    }

    $start = microtime(TRUE);
    $return = $this->checksumInvalidator->getCurrentChecksum($tags);
    $stop = microtime(TRUE);
@@ -46,6 +52,11 @@ public function isValid($checksum, array $tags) {
   * {@inheritdoc}
   */
  public function invalidateTags(array $tags) {
    // If there are no cache tags, there is nothing to invalidate, and the
    // decorated method will be a no-op, so don't log anything.
    if (empty($tags)) {
      return $this->checksumInvalidator->invalidateTags($tags);
    }
    $start = microtime(TRUE);
    $return = $this->checksumInvalidator->invalidateTags($tags);
    $stop = microtime(TRUE);
+3 −2
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public function testAnonymous() {
    $this->assertSame(137, $performance_data->getCacheGetCount());
    $this->assertSame(47, $performance_data->getCacheSetCount());
    $this->assertSame(0, $performance_data->getCacheDeleteCount());
    $this->assertCountBetween(143, 146, $performance_data->getCacheTagChecksumCount());
    $this->assertCountBetween(40, 43, $performance_data->getCacheTagChecksumCount());
    $this->assertCountBetween(47, 50, $performance_data->getCacheTagIsValidCount());
    $this->assertSame(0, $performance_data->getCacheTagInvalidationCount());

@@ -73,7 +73,7 @@ public function testAnonymous() {
    $this->assertSame(95, $performance_data->getCacheGetCount());
    $this->assertSame(16, $performance_data->getCacheSetCount());
    $this->assertSame(0, $performance_data->getCacheDeleteCount());
    $this->assertCountBetween(79, 80, $performance_data->getCacheTagChecksumCount());
    $this->assertCountBetween(24, 25, $performance_data->getCacheTagChecksumCount());
    $this->assertCountBetween(41, 42, $performance_data->getCacheTagIsValidCount());
    $this->assertSame(0, $performance_data->getCacheTagInvalidationCount());

@@ -87,6 +87,7 @@ public function testAnonymous() {
    $this->assertSame(81, $performance_data->getCacheGetCount());
    $this->assertSame(16, $performance_data->getCacheSetCount());
    $this->assertSame(0, $performance_data->getCacheDeleteCount());
    $this->assertCountBetween(24, 25, $performance_data->getCacheTagChecksumCount());
    $this->assertCountBetween(36, 37, $performance_data->getCacheTagIsValidCount());
    $this->assertSame(0, $performance_data->getCacheTagInvalidationCount());
  }