You are here

function KeyvalueMongodb::setWithExpireIfNotExists in MongoDB 8

Sets a value for a given key with a time to live if it does not yet exist.

Parameters

string $key: The key of the data to store.

mixed $value: The data to store.

int $expire: The time to live for items, in seconds.

Return value

bool TRUE if the data was set, or FALSE if it already existed.

Overrides KeyValueStoreExpirableInterface::setWithExpireIfNotExists

File

src/KeyvalueMongodb.php, line 95

Class

KeyvalueMongodb
This class holds a MongoDB key-value backend.

Namespace

Drupal\mongodb

Code

function setWithExpireIfNotExists($key, $value, $expire) {
  try {
    return $this
      ->collection()
      ->insert($this
      ->getObject($key, $value, $expire));
  } catch (\MongoCursorException $e) {
    return FALSE;
  }
}