Esempio n. 1
0
 /**
  * Stores multiple items in the persistent cache.
  *
  * @param array $items
  *   An array of cache items, keyed by cid.
  *
  * @see \Drupal\Core\Cache\CacheBackendInterface::setMultiple()
  */
 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();
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function setMultiple(array $items)
 {
     $values = array();
     foreach ($items as $cid => $item) {
         // All items should have expiration data and initialized tags value.
         $item += array('expire' => CacheBackendInterface::CACHE_PERMANENT, 'tags' => array());
         assert('\\Drupal\\Component\\Assertion\\Inspector::assertAllStrings($item[\'tags\'])', 'Cache Tags must be strings.');
         // Organize tags.
         $item['tags'] = array_unique($item['tags']);
         sort($item['tags']);
         // Create a new object which will be passed to the memcached server for
         // storage.
         $value = new \stdClass();
         $value->cid = $cid;
         $value->expire = $item['expire'];
         $value->created = round(microtime(TRUE), 3);
         $value->tags = $item['tags'];
         $value->checksum = $this->checksumProvider->getCurrentChecksum($item['tags']);
         $value->data = $item['data'];
         $values[] = $value;
     }
     if (empty($values)) {
         return TRUE;
     }
     // Handover set operation to DrupalMemcache(d) object.
     return $this->memcached->setMulti($values, $this->bin);
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function setMultiple(array $items)
 {
     // Use a transaction so that the database can write the changes in a single
     // commit.
     $transaction = $this->connection->startTransaction();
     try {
         // Delete all items first so we can do one insert. Rather than multiple
         // merge queries.
         $this->deleteMultiple(array_keys($items));
         $query = $this->connection->insert($this->bin)->fields(array('cid', 'data', 'expire', 'created', 'serialized', 'tags', 'checksum'));
         foreach ($items as $cid => $item) {
             $item += array('expire' => CacheBackendInterface::CACHE_PERMANENT, 'tags' => array());
             Cache::validateTags($item['tags']);
             $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' => $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;
             }
             $query->values($fields);
         }
         $query->execute();
     } catch (\Exception $e) {
         $transaction->rollback();
         // @todo Log something here or just re throw?
         throw $e;
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = array())
 {
     assert('\\Drupal\\Component\\Assertion\\Inspector::assertAllStrings($tags)', 'Cache tags must be strings.');
     $tags = array_unique($tags);
     $cache = new \stdClass();
     $cache->cid = $cid;
     $cache->created = round(microtime(TRUE), 3);
     $cache->expire = $expire;
     $cache->tags = implode(' ', $tags);
     $cache->checksum = $this->checksumProvider->getCurrentChecksum($tags);
     // APCu serializes/unserializes any structure itself.
     $cache->serialized = 0;
     $cache->data = $data;
     // Expiration is handled by our own prepareItem(), not APCu.
     apcu_store($this->getApcuKey($cid), $cache);
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function setMultiple(array $items)
 {
     $values = array();
     foreach ($items as $cid => $item) {
         $item += array('expire' => CacheBackendInterface::CACHE_PERMANENT, 'tags' => array());
         Cache::validateTags($item['tags']);
         $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' => $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 a transaction so that the database can write the changes in a single
     // commit. The transaction is started after calculating the tag checksums
     // since that can create a table and this causes an exception when using
     // PostgreSQL.
     $transaction = $this->connection->startTransaction();
     try {
         // Delete all items first so we can do one insert. Rather than multiple
         // merge queries.
         $this->deleteMultiple(array_keys($items));
         $query = $this->connection->insert($this->bin)->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();
     } catch (\Exception $e) {
         $transaction->rollback();
         // @todo Log something here or just re throw?
         throw $e;
     }
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = array())
 {
     assert('\\Drupal\\Component\\Assertion\\Inspector::assertAllStrings($tags)', 'Cache tags must be strings.');
     $tags = array_unique($tags);
     $cache = new \stdClass();
     $cache->cid = $cid;
     $cache->created = round(microtime(TRUE), 3);
     $cache->expire = $expire;
     $cache->tags = implode(' ', $tags);
     $cache->checksum = $this->checksumProvider->getCurrentChecksum($tags);
     // APC serializes/unserializes any structure itself.
     $cache->serialized = 0;
     $cache->data = $data;
     // apc_store()'s $ttl argument can be omitted but also set to 0 (zero),
     // in which case the value will persist until it's removed from the cache or
     // until the next cache clear, restart, etc. This is what we want to do
     // when $expire equals CacheBackendInterface::CACHE_PERMANENT.
     if ($expire === CacheBackendInterface::CACHE_PERMANENT) {
         $expire = 0;
     }
     apc_store($this->getApcuKey($cid), $cache, $expire);
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array())
 {
     assert('\\Drupal\\Component\\Assertion\\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
     $item = (object) array('cid' => $cid, 'data' => $data, 'created' => round(microtime(TRUE), 3), 'expire' => $expire, 'tags' => array_unique($tags), 'checksum' => $this->checksumProvider->getCurrentChecksum($tags));
     $this->writeItem($this->normalizeCid($cid), $item);
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array())
 {
     Cache::validateTags($tags);
     $item = (object) array('cid' => $cid, 'data' => $data, 'created' => round(microtime(TRUE), 3), 'expire' => $expire, 'tags' => array_unique($tags), 'checksum' => $this->checksumProvider->getCurrentChecksum($tags));
     $this->writeItem($this->normalizeCid($cid), $item);
 }