function LruMemoryCacheTest::testGetSetDelete

Tests getting, setting and deleting items from the LRU memory cache.

@covers ::get @covers ::set @covers ::delete @covers ::getMultiple

File

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

Class

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

Namespace

Drupal\Tests\Core\Cache

Code

public function testGetSetDelete() : 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->set('cuckoo', 'cuckoo');
    $this->assertCacheData($lru_cache, [
        [
            'pigeon',
            'pigeon',
        ],
        [
            'crow',
            'crow',
        ],
        [
            'cuckoo',
            'cuckoo',
        ],
    ]);
    // Now bring pigeon to the most recently used spot.
    $lru_cache->get('pigeon');
    $this->assertCacheData($lru_cache, [
        [
            'crow',
            'crow',
        ],
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
    ]);
    // Confirm that setting the same item multiple times only uses one slot.
    $lru_cache->set('bigger_cuckoo', 'bigger_cuckoo');
    $lru_cache->set('bigger_cuckoo', 'bigger_cuckoo');
    $lru_cache->set('bigger_cuckoo', 'bigger_cuckoo');
    $lru_cache->set('bigger_cuckoo', 'bigger_cuckoo');
    $lru_cache->set('bigger_cuckoo', 'bigger_cuckoo');
    $this->assertCacheData($lru_cache, [
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
        [
            'bigger_cuckoo',
            'bigger_cuckoo',
        ],
    ]);
    // Confirm that deleting the same item multiple times only frees up one
    // slot.
    $lru_cache->delete('bigger_cuckoo');
    $lru_cache->delete('bigger_cuckoo');
    $lru_cache->delete('bigger_cuckoo');
    $lru_cache->delete('bigger_cuckoo');
    $lru_cache->delete('bigger_cuckoo');
    $lru_cache->delete('bigger_cuckoo');
    $this->assertCacheData($lru_cache, [
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
    ]);
    $lru_cache->set('crow', 'crow');
    $this->assertCacheData($lru_cache, [
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
        [
            'crow',
            'crow',
        ],
    ]);
    // Ensure nothing changes on cache miss for ::get().
    $this->assertFalse($lru_cache->get('dodo'));
    $this->assertCacheData($lru_cache, [
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
        [
            'crow',
            'crow',
        ],
    ]);
    // Ensure nothing changes on cache miss for ::getMultiple().
    $cids = [
        'dodo',
        'great_auk',
    ];
    $this->assertEmpty($lru_cache->getMultiple($cids));
    $this->assertCacheData($lru_cache, [
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
        [
            'crow',
            'crow',
        ],
    ]);
    $this->assertSame([
        'dodo',
        'great_auk',
    ], $cids);
    $cids = [
        'pigeon',
        'cuckoo',
    ];
    $lru_cache->getMultiple($cids);
    // @todo This result suggests the order of the arguments in the
    //   \Drupal\Core\Cache\MemoryBackend::getMultiple() array_intersect_key()
    //   should be swapped as this order of the cache items returned should
    //   probably be in the same order as the passed in $cache_data. I.e. cuckoo
    //   should be at the ends of the array and not crow.
    $this->assertCacheData($lru_cache, [
        [
            'crow',
            'crow',
        ],
        [
            'cuckoo',
            'cuckoo',
        ],
        [
            'pigeon',
            'pigeon',
        ],
    ]);
    $this->assertEmpty($cids);
}

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