function CacheCollector::updateCache

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/CacheCollector.php \Drupal\Core\Cache\CacheCollector::updateCache()
  2. 8.9.x core/lib/Drupal/Core/Cache/CacheCollector.php \Drupal\Core\Cache\CacheCollector::updateCache()
  3. 11.x core/lib/Drupal/Core/Cache/CacheCollector.php \Drupal\Core\Cache\CacheCollector::updateCache()

Writes a value to the persistent cache immediately.

Parameters

bool $lock: (optional) Whether to acquire a lock before writing to cache. Defaults to TRUE.

2 calls to CacheCollector::updateCache()
CacheCollector::destruct in core/lib/Drupal/Core/Cache/CacheCollector.php
Performs destruct operations.
State::updateCache in core/lib/Drupal/Core/State/State.php
Writes a value to the persistent cache immediately.
2 methods override CacheCollector::updateCache()
State::updateCache in core/lib/Drupal/Core/State/State.php
Writes a value to the persistent cache immediately.
ThemeRegistry::updateCache in core/lib/Drupal/Core/Utility/ThemeRegistry.php
Writes a value to the persistent cache immediately.

File

core/lib/Drupal/Core/Cache/CacheCollector.php, line 222

Class

CacheCollector
Default implementation for CacheCollectorInterface.

Namespace

Drupal\Core\Cache

Code

protected function updateCache($lock = TRUE) {
    $data = [];
    foreach ($this->keysToPersist as $offset => $persist) {
        if ($persist) {
            $data[$offset] = $this->storage[$offset];
        }
    }
    if (empty($data) && empty($this->keysToRemove)) {
        return;
    }
    // Lock cache writes to help avoid stampedes.
    $cid = $this->getCid();
    $lock_name = $cid . ':' . __CLASS__;
    if (!$lock || $this->lock
        ->acquire($lock_name)) {
        // Set and delete operations invalidate the cache item. Try to also load
        // an eventually invalidated cache entry, only update an invalidated cache
        // entry if the creation date did not change as this could result in an
        // inconsistent cache.
        if ($cache = $this->cache
            ->get($cid, $this->cacheInvalidated)) {
            if ($this->cacheInvalidated && $cache->created != $this->cacheCreated) {
                // We have invalidated the cache in this request and got a different
                // cache entry. Do not attempt to overwrite data that might have been
                // changed in a different request. We'll let the cache rebuild in
                // later requests.
                $this->cache
                    ->delete($cid);
                $this->lock
                    ->release($lock_name);
                return;
            }
            // If there wasn't a cache item at the beginning of the request, but
            // there is now, then there has been a cache write in the interim.
            // Discard our data if so since the cache may have been written by
            // a request that was also setting data.
            if (!$this->cacheCreated) {
                return;
            }
            $data = array_merge($cache->data, $data);
        }
        elseif ($this->cacheCreated) {
            // Getting here indicates that there was a cache entry at the
            // beginning of the request, but now it's gone (some other process
            // must have cleared it). We back out to prevent corrupting the cache
            // with incomplete data, since we won't be able to properly merge
            // the existing cache data from earlier with the new data.
            // A future request will properly hydrate the cache from scratch.
            if ($lock) {
                $this->lock
                    ->release($lock_name);
            }
            return;
        }
        // Remove keys marked for deletion.
        foreach ($this->keysToRemove as $delete_key) {
            unset($data[$delete_key]);
        }
        $this->cache
            ->set($cid, $data, Cache::PERMANENT, $this->tags);
        if ($lock) {
            $this->lock
                ->release($lock_name);
        }
    }
    $this->keysToPersist = [];
    $this->keysToRemove = [];
}

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