function LruMemoryCache::set

Overrides MemoryCache::set

File

core/lib/Drupal/Core/Cache/MemoryCache/LruMemoryCache.php, line 75

Class

LruMemoryCache
Defines a least recently used (LRU) static cache implementation.

Namespace

Drupal\Core\Cache\MemoryCache

Code

public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) : void {
  if (isset($this->cache[$cid])) {
    // If the item is already in the cache, move it to end of the array.
    unset($this->cache[$cid]);
  }
  elseif (count($this->cache) > $this->allowedSlots - 1) {
    // Remove one item from the cache to ensure we remain within the allowed
    // number of slots. Avoid using array_slice() because it makes a copy of
    // the array, and avoid using array_splice() or array_shift() because they
    // re-index numeric keys.
    unset($this->cache[array_key_first($this->cache)]);
  }
  parent::set($cid, $data, $expire, $tags);
}

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