You are here

public function DatabaseStorageExpirable::setWithExpireIfNotExists in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php \Drupal\Core\KeyValueStore\DatabaseStorageExpirable::setWithExpireIfNotExists()

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

If a key is expired it also does not exists.

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

core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php, line 161

Class

DatabaseStorageExpirable
Defines a default key/value store implementation for expiring items.

Namespace

Drupal\Core\KeyValueStore

Code

public function setWithExpireIfNotExists($key, $value, $expire) {
  try {
    return $this
      ->doSetWithExpireIfNotExists($key, $value, $expire);
  } catch (\Exception $e) {

    // If there was an exception, try to create the table.
    if ($this
      ->ensureTableExists()) {
      return $this
        ->doSetWithExpireIfNotExists($key, $value, $expire);
    }
    else {
      throw $e;
    }
  }
}