You are here

function dmemcache_get_multi in Memcache API and Integration 6

Same name and namespace in other branches
  1. 7 dmemcache.inc \dmemcache_get_multi()

Retrieve multiple values from the cache.

Parameters

$keys The keys with which the items were stored.:

$bin The bin in which the item was stored.:

Return value

The item which was originally saved or FALSE

2 calls to dmemcache_get_multi()
memcache_wildcards in ./memcache.inc
Utilize multiget to retrieve all possible wildcard matches, storing statically so multiple cache requests for the same item on the same page load doesn't add overhead.
_dmemcache_get_pieces in ./dmemcache.inc
Retrieve a value from the cache.

File

./dmemcache.inc, line 338

Code

function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) {
  $collect_stats = dmemcache_stats_init();
  $multi_stats = array();
  $full_keys = array();
  foreach ($keys as $key => $cid) {
    $full_key = dmemcache_key($cid, $bin);
    $full_keys[$cid] = $full_key;
    if ($collect_stats) {
      $multi_stats[$full_key] = FALSE;
    }
  }
  $results = array();
  if ($mc || ($mc = dmemcache_object($bin))) {
    if ($mc instanceof Memcached) {
      if (PHP_MAJOR_VERSION >= 7) {

        // See https://www.drupal.org/node/2728427
        $results = $mc
          ->getMulti($full_keys, Memcached::GET_PRESERVE_ORDER);
      }
      else {
        $cas_tokens = NULL;
        $results = $mc
          ->getMulti($full_keys, $cas_tokens, Memcached::GET_PRESERVE_ORDER);
      }
    }
    elseif ($mc instanceof Memcache) {
      $track_errors = ini_set('track_errors', '1');
      $php_errormsg = '';
      $results = @$mc
        ->get($full_keys);

      // Order is not guaranteed, map responses to order of requests.
      $keys = array_fill_keys($full_keys, NULL);
      $results = array_merge($keys, $results);
      if (!empty($php_errormsg)) {
        register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get_multi: !msg', array(
          '!msg' => $php_errormsg,
        ), WATCHDOG_WARNING);
        $php_errormsg = '';
      }
      ini_set('track_errors', $track_errors);
    }
  }

  // If $results is FALSE, convert it to an empty array.
  if (!$results) {
    $results = array();
  }
  if ($collect_stats) {
    foreach ($multi_stats as $key => $value) {
      $multi_stats[$key] = isset($results[$key]) ? TRUE : FALSE;
    }
  }

  // Convert the full keys back to the cid.
  $cid_results = array();
  $cid_lookup = array_flip($full_keys);
  foreach ($results as $key => $value) {
    if (is_object($value) && !empty($value->multi_part_data)) {

      // This is a multi-part value.
      $value = _dmemcache_get_pieces($value->data, $value->cid, $bin, $mc);
    }
    $cid_results[$cid_lookup[$key]] = $value;
  }
  if ($collect_stats) {
    dmemcache_stats_write('getMulti', $bin, $multi_stats);
  }
  return $cid_results;
}