You are here

public function DrupalMemcached::setMulti in Memcache Storage 8

Bulk set if cache items to the memcached pool.

Parameters

array $items: List of cache items.

string $cache_bin: Name of the cache bin.

Overrides DrupalMemcachedInterface::setMulti

File

src/DrupalMemcached.php, line 97

Class

DrupalMemcached
Class DrupalMemcached

Namespace

Drupal\memcache_storage

Code

public function setMulti(array $items, $cache_bin = '') {

  // No point in performing any action is we're not connected to memcached.
  if (empty($this->isConnected)) {
    return;
  }

  // To perform a multiple set operation we have to group cache items
  // by the expiration time.
  // See http://php.net/manual/en/memcached.setmulti.php
  $item_groups = [];
  foreach ($items as $item) {

    // Get the formatted cache key.
    $memcached_key = $this
      ->itemKey($item->cid, $cache_bin);

    // Prepare the expiration time for memcached.
    $expiration = $item->expire;
    if ($item->expire == CacheBackendInterface::CACHE_PERMANENT) {
      $expiration = 0;
    }
    $item_groups[$expiration][$memcached_key] = $item;
  }

  // Multiple set of cache items.
  foreach ($item_groups as $expiration => $cache_items) {

    // Perform preparations for the debug logging.
    if (!empty($this->debug)) {
      DrupalMemcachedDebug::prepare();

      // Prepare the matching between memcached keys and drupal cache ids.
      $memcached_keys = [];
      foreach ($cache_items as $cache_key => $item) {
        $memcached_keys[$cache_key] = $item->cid;
      }
    }
    $result = $this->memcached
      ->setMulti($cache_items, $expiration);

    // Logs the debug entry about the memcached operation.
    if (!empty($this->debug)) {
      DrupalMemcachedDebug::process('set', $result, $memcached_keys, $cache_bin, $this->cluster);
    }
  }
}