You are here

public function Predis::getMultiple in Redis 8

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

src/Cache/Predis.php, line 38

Class

Predis
Predis cache backend.

Namespace

Drupal\redis\Cache

Code

public function getMultiple(&$cids, $allow_invalid = FALSE) {

  // Avoid an error when there are no cache ids.
  if (empty($cids)) {
    return [];
  }
  $return = [];

  // Build the list of keys to fetch.
  $keys = array_map([
    $this,
    'getKey',
  ], $cids);

  // Optimize for the common case when only a single cache entry needs to
  // be fetched, no pipeline is needed then.
  if (count($keys) > 1) {
    $pipe = $this->client
      ->pipeline();
    foreach ($keys as $key) {
      $pipe
        ->hgetall($key);
    }
    $result = $pipe
      ->execute();
  }
  else {
    $result = [
      $this->client
        ->hGetAll(reset($keys)),
    ];
  }

  // Loop over the cid values to ensure numeric indexes.
  foreach (array_values($cids) as $index => $key) {

    // Check if a valid result was returned from Redis.
    if (isset($result[$index]) && is_array($result[$index])) {

      // Check expiration and invalidation and convert into an object.
      $item = $this
        ->expandEntry($result[$index], $allow_invalid);
      if ($item) {
        $return[$item->cid] = $item;
      }
    }
  }

  // Remove fetched cids from the list.
  $cids = array_diff($cids, array_keys($return));
  return $return;
}