You are here

public function BackendChain::getMultiple in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Cache/BackendChain.php \Drupal\Core\Cache\BackendChain::getMultiple()

Returns data from the persistent cache when given an array of cache IDs.

Parameters

array $cids: An array of cache IDs for the data to retrieve. This is passed by reference, and will have the IDs successfully returned from cache removed.

bool $allow_invalid: (optional) If TRUE, cache items may be returned even if they have expired or been invalidated. Such items may sometimes be preferred, if the alternative is recalculating the value stored in the cache, especially if another concurrent thread is already recalculating the same value. The "valid" property of the returned objects indicates whether the items are valid or not. Defaults to FALSE.

Return value

array An array of cache item objects indexed by cache ID.

Overrides CacheBackendInterface::getMultiple

See also

\Drupal\Core\Cache\CacheBackendInterface::get()

File

core/lib/Drupal/Core/Cache/BackendChain.php, line 85

Class

BackendChain
Defines a chained cache implementation for combining multiple cache backends.

Namespace

Drupal\Core\Cache

Code

public function getMultiple(&$cids, $allow_invalid = FALSE) {
  $return = [];
  foreach ($this->backends as $index => $backend) {
    $items = $backend
      ->getMultiple($cids, $allow_invalid);

    // Propagate the values that could be retrieved from the current cache
    // backend to all missed backends.
    if ($index > 0 && !empty($items)) {
      for ($i = $index - 1; 0 <= $i; --$i) {
        foreach ($items as $cached) {
          $this->backends[$i]
            ->set($cached->cid, $cached->data, $cached->expire, $cached->tags);
        }
      }
    }

    // Append the values to the previously retrieved ones.
    $return += $items;
    if (empty($cids)) {

      // No need to go further if we don't have any cid to fetch left.
      break;
    }
  }
  return $return;
}