function ChainedFastBackend::set

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Cache/ChainedFastBackend.php \Drupal\Core\Cache\ChainedFastBackend::set()
  2. 10 core/lib/Drupal/Core/Cache/ChainedFastBackend.php \Drupal\Core\Cache\ChainedFastBackend::set()
  3. 9 core/lib/Drupal/Core/Cache/ChainedFastBackend.php \Drupal\Core\Cache\ChainedFastBackend::set()
  4. 8.9.x core/lib/Drupal/Core/Cache/ChainedFastBackend.php \Drupal\Core\Cache\ChainedFastBackend::set()

Stores data in the persistent cache.

Core cache implementations set the created time on cache item with microtime(TRUE) rather than REQUEST_TIME_FLOAT, because the created time of cache items should match when they are created, not when the request started. Apart from being more accurate, this increases the chance an item will legitimately be considered valid.

Parameters

string $cid: The cache ID of the data to store.

mixed $data: The data to store in the cache. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.

int $expire: One of the following values:

  • CacheBackendInterface::CACHE_PERMANENT: Indicates that the item should not be removed unless it is deleted explicitly.
  • A Unix timestamp: Indicates that the item will be considered invalid after this time, i.e. it will not be returned by get() unless $allow_invalid has been set to TRUE. When the item has expired, it may be permanently deleted by the garbage collector at any time.

array $tags: An array of tags to be stored with the cache item. These should normally identify objects used to build the cache item, which should trigger cache invalidation when updated. For example if a cached item represents a node, both the node ID and the author's user ID might be passed in as tags. For example ['node:123', 'node:456', 'user:789'].

Overrides CacheBackendInterface::set

File

core/lib/Drupal/Core/Cache/ChainedFastBackend.php, line 204

Class

ChainedFastBackend
Defines a backend with a fast and a consistent backend chain.

Namespace

Drupal\Core\Cache

Code

public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
  // When setting a cache item, if the cache item already exists in the
  // consistent backend then the fast backend must be invalidated. However,
  // if the item does not exist in the consistent backend at all, it will also
  // be new on the fast backend and we can skip invalidating the entire bin.
  //
  // Because there is no way inherently to know whether an item is being added
  // or updated when it is set, first get the item from the persistent backend
  // then act based on that. There will be a small window between getting the
  // item and setting it where another process may have set a value, but that
  // process will also have previously got an empty cache item so the risk of
  // a race condition resulting in different values is extremely low.
  //
  // In both cases, don't bother writing back to the fast backend - the next
  // call to ::get() with this cache ID will do that.
  $cached = $this->consistentBackend
    ->get($cid);
  $this->consistentBackend
    ->set($cid, $data, $expire, $tags);
  if ($cached) {
    $this->markAsOutdated();
  }
}

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