You are here

public function SessionBasedTempStore::set in Session Based Temporary Storage 8

Stores a particular key/value pair in this PrivateTempStore.

Parameters

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

mixed $value: The data to store.

Throws

\Drupal\Core\TempStore\TempStoreException Thrown when a lock for the backend storage could not be acquired.

File

src/SessionBasedTempStore.php, line 176

Class

SessionBasedTempStore
Stores and retrieves temporary data for a given owner.

Namespace

Drupal\session_based_temp_store

Code

public function set($key, $value) {
  $key = $this
    ->createkey($key);
  if (!$this->lockBackend
    ->acquire($key)) {
    $this->lockBackend
      ->wait($key);
    if (!$this->lockBackend
      ->acquire($key)) {
      throw new TempStoreException("Couldn't acquire lock to update item '{$key}' in '{$this->storage->getCollectionName()}' session based temporary storage.");
    }
  }
  $value = (object) [
    'owner' => $this
      ->getOwner(),
    'data' => $value,
    'updated' => (int) $this->requestStack
      ->getMasterRequest()->server
      ->get('REQUEST_TIME'),
  ];

  // If the global expiration time is set to 0 (expire at the end of the session),
  // Let's set the DB storage entry expiration time to 24 hours.
  $expire = $this->expire === 0 ? 86400 : $this->expire;
  $this->storage
    ->setWithExpire($key, $value, $expire);
  $this->lockBackend
    ->release($key);
}