function LruMemoryCacheTest::testInvalidateNumeric

Tests invalidation with numeric keys from the LRU memory cache.

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

File

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

Class

LruMemoryCacheTest
@coversDefaultClass \Drupal\Core\Cache\MemoryCache\LruMemoryCache @group Cache

Namespace

Drupal\Tests\Core\Cache

Code

public function testInvalidateNumeric() : void {
    $lru_cache = $this->getLruMemoryCache(3);
    $cache_data = [
        [
            3,
            'sparrow',
        ],
        [
            10,
            'pigeon',
        ],
        [
            5,
            'crow',
        ],
    ];
    foreach ($cache_data as $items) {
        $lru_cache->set($items[0], $items[1], tags: [
            'bird',
        ]);
    }
    $this->assertCacheData($lru_cache, [
        [
            3,
            'sparrow',
        ],
        [
            10,
            'pigeon',
        ],
        [
            5,
            'crow',
        ],
    ]);
    // Invalidate something not in the cache and ensure nothing changes.
    $lru_cache->invalidate(0);
    $this->assertCacheData($lru_cache, [
        [
            3,
            'sparrow',
        ],
        [
            10,
            'pigeon',
        ],
        [
            5,
            'crow',
        ],
    ]);
    $lru_cache->invalidate(10);
    $this->assertCacheData($lru_cache, [
        [
            10,
            'pigeon',
        ],
        [
            3,
            'sparrow',
        ],
        [
            5,
            'crow',
        ],
    ]);
    $this->assertFalse($lru_cache->get(10));
    $this->assertSame('pigeon', $lru_cache->get(10, TRUE)->data);
    $lru_cache->invalidateTags([
        'mammal',
    ]);
    $this->assertCacheData($lru_cache, [
        [
            10,
            'pigeon',
        ],
        [
            3,
            'sparrow',
        ],
        [
            5,
            'crow',
        ],
    ]);
    $this->assertSame('sparrow', $lru_cache->get(3)->data);
    $this->assertCacheData($lru_cache, [
        [
            10,
            'pigeon',
        ],
        [
            5,
            'crow',
        ],
        [
            3,
            'sparrow',
        ],
    ]);
    $lru_cache->invalidateTags([
        'mammal',
        'bird',
    ]);
    $this->assertFalse($lru_cache->get(3));
    $this->assertFalse($lru_cache->get(10));
    $this->assertFalse($lru_cache->get(5));
    $this->assertCacheData($lru_cache, [
        [
            10,
            'pigeon',
        ],
        [
            5,
            'crow',
        ],
        [
            3,
            'sparrow',
        ],
    ]);
}

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