function CacheCollector::updateCache

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Cache/CacheCollector.php \Drupal\Core\Cache\CacheCollector::updateCache()
  2. 10 core/lib/Drupal/Core/Cache/CacheCollector.php \Drupal\Core\Cache\CacheCollector::updateCache()
  3. 9 core/lib/Drupal/Core/Cache/CacheCollector.php \Drupal\Core\Cache\CacheCollector::updateCache()
  4. 8.9.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.

5 calls to CacheCollector::updateCache()
CacheCollector::destruct in core/lib/Drupal/Core/Cache/CacheCollector.php
FileParsingCacheCollectorBase::updateCache in core/lib/Drupal/Core/Utility/FileParsingCacheCollectorBase.php
MenuActiveTrail::destruct in core/lib/Drupal/Core/Menu/MenuActiveTrail.php
State::set in core/lib/Drupal/Core/State/State.php
ThemeRegistry::updateCache in core/lib/Drupal/Core/Utility/ThemeRegistry.php
2 methods override CacheCollector::updateCache()
FileParsingCacheCollectorBase::updateCache in core/lib/Drupal/Core/Utility/FileParsingCacheCollectorBase.php
ThemeRegistry::updateCache in core/lib/Drupal/Core/Utility/ThemeRegistry.php

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. Try to acquire the lock, but
  // continue even if it cannot be acquired: a ::set() or ::delete() during
  // this request may have invalidated the cache item, and that invalidation
  // must be propagated whether or not the lock is held. Only the cache write
  // itself depends on the lock.
  $cid = $this->getCid();
  $lock_name = $cid . ':' . __CLASS__;
  $lock_acquired = FALSE;
  if ($lock) {
    $lock_acquired = $this->lock
      ->acquire($lock_name);
  }
  // Set and delete operations invalidate the cache item, so load an
  // invalidated cache entry too if this request invalidated it. Comparing
  // the content detects changes to the content; if it changed, another
  // request is responsible for it and this request must not overwrite that
  // work.
  $cache = $this->cache
    ->get($cid, $this->cacheInvalidated);
  $fingerprint = $cache ? $this->fingerprint($cache->data) : NULL;
  $loaded_fingerprint = $this->loadedData === NULL ? NULL : $this->fingerprint($this->loadedData);
  $unchanged = $fingerprint === $loaded_fingerprint;
  if ($cache && $this->cacheInvalidated) {
    // This request invalidated the cache item via ::set() or ::delete() and a
    // cache item still exists. Delete it so that a later request rebuilds it
    // cleanly; the up-to-date value is written within a lock by the caller
    // (see ::set()). This happens whether or not the lock was acquired.
    $this->cache
      ->delete($cid);
  }
  elseif ($unchanged && (!$lock || $lock_acquired)) {
    // The cache item is in the same state as when it was loaded, so it is
    // safe to write back the data collected during this request, merged with
    // any existing cache data and with deleted keys removed.
    if ($cache) {
      $data = array_merge($cache->data, $data);
    }
    foreach ($this->keysToRemove as $delete_key) {
      unset($data[$delete_key]);
    }
    $this->cache
      ->set($cid, $data, Cache::PERMANENT, $this->tags);
  }
  if ($lock_acquired) {
    $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.