function LruMemoryCacheTest::testInvalidate

Tests invalidation from the LRU memory cache.

@covers ::invalidate
@covers ::invalidateMultiple
@covers ::invalidateTags

File

core/tests/Drupal/Tests/Core/Cache/LruMemoryCacheTest.php, line 213

Class

LruMemoryCacheTest
@coversDefaultClass \Drupal\Core\Cache\MemoryCache\LruMemoryCache[[api-linebreak]] @group Cache

Namespace

Drupal\Tests\Core\Cache

Code

public function testInvalidate() : void {
  $lru_cache = $this->getLruMemoryCache(3);
  $cache_data = [
    [
      'sparrow',
      'sparrow',
    ],
    [
      'pigeon',
      'pigeon',
    ],
    [
      'crow',
      'crow',
    ],
  ];
  foreach ($cache_data as $items) {
    $lru_cache->set($items[0], $items[1]);
  }
  $this->assertCacheData($lru_cache, [
    [
      'sparrow',
      'sparrow',
    ],
    [
      'pigeon',
      'pigeon',
    ],
    [
      'crow',
      'crow',
    ],
  ]);
  $lru_cache->invalidate('crow');
  $this->assertCacheData($lru_cache, [
    [
      'crow',
      'crow',
    ],
    [
      'sparrow',
      'sparrow',
    ],
    [
      'pigeon',
      'pigeon',
    ],
  ]);
  $this->assertFalse($lru_cache->get('crow'));
  // Ensure that getting an invalid cache does not move it to the end of the
  // array.
  $this->assertSame('crow', $lru_cache->get('crow', TRUE)->data);
  $this->assertCacheData($lru_cache, [
    [
      'crow',
      'crow',
    ],
    [
      'sparrow',
      'sparrow',
    ],
    [
      'pigeon',
      'pigeon',
    ],
  ]);
  $lru_cache->set('cuckoo', 'cuckoo', LruMemoryCache::CACHE_PERMANENT, [
    'cuckoo',
  ]);
  $this->assertCacheData($lru_cache, [
    [
      'sparrow',
      'sparrow',
    ],
    [
      'pigeon',
      'pigeon',
    ],
    [
      'cuckoo',
      'cuckoo',
    ],
  ]);
  $lru_cache->invalidateTags([
    'cuckoo',
  ]);
  $this->assertFalse($lru_cache->get('cuckoo'));
  $this->assertSame('cuckoo', $lru_cache->get('cuckoo', TRUE)->data);
  $lru_cache->set('crow', 'crow');
  $this->assertCacheData($lru_cache, [
    [
      'sparrow',
      'sparrow',
    ],
    [
      'pigeon',
      'pigeon',
    ],
    [
      'crow',
      'crow',
    ],
  ]);
  $lru_cache->invalidateMultiple([
    'pigeon',
    'crow',
  ]);
  $cids = [
    'pigeon',
    'crow',
  ];
  $this->assertEmpty($lru_cache->getMultiple($cids));
  $this->assertSame([
    'pigeon',
    'crow',
  ], $cids);
  $this->assertCount(2, $lru_cache->getMultiple($cids, TRUE));
  $this->assertSame([], $cids);
  $this->assertCacheData($lru_cache, [
    [
      'pigeon',
      'pigeon',
    ],
    [
      'crow',
      'crow',
    ],
    [
      'sparrow',
      'sparrow',
    ],
  ]);
  $lru_cache->set('duck', 'duck');
  $lru_cache->set('chicken', 'chicken');
  $this->assertCacheData($lru_cache, [
    [
      'sparrow',
      'sparrow',
    ],
    [
      'duck',
      'duck',
    ],
    [
      'chicken',
      'chicken',
    ],
  ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.