protected function DatabaseBackend::doSetMultiple

protected DatabaseBackend::doSetMultiple(array $items)

Stores multiple items in the persistent cache.

Parameters

array $items: An array of cache items, keyed by cid.

See also

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

File

core/lib/Drupal/Core/Cache/DatabaseBackend.php, line 188

Class

DatabaseBackend
Defines a default cache implementation.

Namespace

Drupal\Core\Cache

Code

protected function doSetMultiple(array $items) {
  $values = array();

  foreach ($items as $cid => $item) {
    $item += array(
      'expire' => CacheBackendInterface::CACHE_PERMANENT,
      'tags' => array(),
    );

    assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($item[\'tags\'])', 'Cache Tags must be strings.');
    $item['tags'] = array_unique($item['tags']);
    // Sort the cache tags so that they are stored consistently in the DB.
    sort($item['tags']);

    $fields = array(
      'cid' => $this->normalizeCid($cid),
      'expire' => $item['expire'],
      'created' => round(microtime(TRUE), 3),
      'tags' => implode(' ', $item['tags']),
      'checksum' => $this->checksumProvider->getCurrentChecksum($item['tags']),
    );

    if (!is_string($item['data'])) {
      $fields['data'] = serialize($item['data']);
      $fields['serialized'] = 1;
    }
    else {
      $fields['data'] = $item['data'];
      $fields['serialized'] = 0;
    }
    $values[] = $fields;
  }

  // Use an upsert query which is atomic and optimized for multiple-row
  // merges.
  $query = $this->connection
    ->upsert($this->bin)
    ->key('cid')
    ->fields(array('cid', 'expire', 'created', 'tags', 'checksum', 'data', 'serialized'));
  foreach ($values as $fields) {
    // Only pass the values since the order of $fields matches the order of
    // the insert fields. This is a performance optimization to avoid
    // unnecessary loops within the method.
    $query->values(array_values($fields));
  }

  $query->execute();
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Cache!DatabaseBackend.php/function/DatabaseBackend::doSetMultiple/8.1.x