You are here

public function Cache::set in MongoDB 7

File

mongodb_cache/mongodb_cache_plugin.php, line 313

Class

Cache
MongoDB cache implementation.

Namespace

Drupal\mongodb_cache

Code

public function set($cid, $data, $expire = CACHE_PERMANENT) {
  $scalar = is_scalar($data);
  $entry = array(
    '_id' => (string) $cid,
    'cid' => (string) $cid,
    'created' => REQUEST_TIME,
    'expire' => $expire,
    'serialized' => !$scalar,
    'data' => $scalar ? $data : serialize($data),
  );

  // Use MongoBinData for non-UTF8 strings.
  if (is_string($entry['data']) && !drupal_validate_utf8($entry['data'])) {
    $entry['data'] = $this
      ->createBinData($entry['data']);
  }
  try {
    $this->collection
      ->save($entry, $this->unsafe);
  } catch (\MongoException $e) {
    self::notifyException($e);

    // The database may not be available, so we'll ignore cache_set requests.
  }
}