You are here

public function KeyValueStoreExpirable::setWithExpire in MongoDB 8.2

Saves a value for a given key with a time to live.

This does not need microsecond precision, since expires happen with only a multi-second accuracy at best.

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.

Overrides KeyValueStoreExpirableInterface::setWithExpire

1 call to KeyValueStoreExpirable::setWithExpire()
KeyValueStoreExpirable::setMultipleWithExpire in modules/mongodb_storage/src/KeyValueStoreExpirable.php
Saves an array of values with a time to live.

File

modules/mongodb_storage/src/KeyValueStoreExpirable.php, line 116

Class

KeyValueStoreExpirable
KeyValueStore provides a KeyValueStoreExpirable as a MongoDB collection.

Namespace

Drupal\mongodb_storage

Code

public function setWithExpire($key, $value, $expire) {
  $selector = [
    '_id' => $this
      ->stringifyKey($key),
  ];
  $replacement = $selector + [
    'expire' => $this
      ->getBsonExpire($expire),
    'value' => serialize($value),
  ];
  $options = [
    'upsert' => TRUE,
  ];
  $this->mongoDbCollection
    ->replaceOne($selector, $replacement, $options);
}