class SharedTempStore (View source)

Stores and retrieves temporary data for a given owner.

A SharedTempStore can be used to make temporary, non-cache data available across requests. The data for the SharedTempStore is stored in one key/value collection. SharedTempStore data expires automatically after a given timeframe.

The SharedTempStore is different from a cache, because the data in it is not yet saved permanently and so it cannot be rebuilt. Typically, the SharedTempStore might be used to store work in progress that is later saved permanently elsewhere, e.g. autosave data, multistep forms, or in-progress changes to complex configuration that are not ready to be saved.

Each SharedTempStore belongs to a particular owner (e.g. a user, session, or process). Multiple owners may use the same key/value collection, and the owner is stored along with the key/value pair.

Every key is unique within the collection, so the SharedTempStore can check whether a particular key is already set by a different owner. This is useful for informing one owner that the data is already in use by another; for example, to let one user know that another user is in the process of editing certain data, or even to restrict other users from editing it at the same time. It is the responsibility of the implementation to decide when and whether one owner can use or update another owner's data.

If you want to be able to ensure that the data belongs to the current user, use \Drupal\Core\TempStore\PrivateTempStore.

Traits

Provides dependency injection friendly methods for serialization.

Properties

protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected KeyValueStoreExpirableInterface $storage

The key/value storage object used for this data.

protected LockBackendInterface $lockBackend

The lock object used for this data.

protected RequestStack $requestStack

The request stack.

protected mixed $owner

The owner key to store along with the data (e.g. a user or session ID).

protected AccountProxyInterface $currentUser

The current user.

protected int $expire

The time to live for items in seconds.

Methods

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, mixed $owner, RequestStack $request_stack, AccountProxyInterface $current_user = NULL, int $expire = 604800)

Constructs a new object for accessing data from a key/value store.

mixed
get(string $key)

Retrieves a value from this SharedTempStore for a given key.

mixed
getIfOwner(string $key)

Retrieves a value from this SharedTempStore for a given key.

bool
setIfNotExists(string $key, mixed $value)

Stores a particular key/value pair only if the key doesn't already exist.

bool
setIfOwner(string $key, mixed $value)

Stores a particular key/value pair in this SharedTempStore.

set(string $key, mixed $value)

Stores a particular key/value pair in this SharedTempStore.

Lock|null
getMetadata(string $key)

Returns the metadata associated with a particular key/value pair.

delete(string $key)

Deletes data from the store for a given key and releases the lock on it.

bool
deleteIfOwner(string $key)

Deletes data from the store for a given key and releases the lock on it.

ensureAnonymousSession()

Stores the owner in the session if the user is anonymous.

Details

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, mixed $owner, RequestStack $request_stack, AccountProxyInterface $current_user = NULL, int $expire = 604800)

Constructs a new object for accessing data from a key/value store.

Parameters

KeyValueStoreExpirableInterface $storage

The key/value storage object used for this data. Each storage object represents a particular collection of data and will contain any number of key/value pairs.

LockBackendInterface $lock_backend

The lock object used for this data.

mixed $owner

The owner key to store along with the data (e.g. a user or session ID).

RequestStack $request_stack

The request stack.

AccountProxyInterface $current_user

The current user.

int $expire

The time to live for items, in seconds.

mixed get(string $key)

Retrieves a value from this SharedTempStore for a given key.

Parameters

string $key

The key of the data to retrieve.

Return Value

mixed

The data associated with the key, or NULL if the key does not exist.

mixed getIfOwner(string $key)

Retrieves a value from this SharedTempStore for a given key.

Only returns the value if the value is owned by $this->owner.

Parameters

string $key

The key of the data to retrieve.

Return Value

mixed

The data associated with the key, or NULL if the key does not exist.

bool setIfNotExists(string $key, mixed $value)

Stores a particular key/value pair only if the key doesn't already exist.

Parameters

string $key

The key of the data to check and store.

mixed $value

The data to store.

Return Value

bool

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

bool setIfOwner(string $key, mixed $value)

Stores a particular key/value pair in this SharedTempStore.

Only stores the given key/value pair if it does not exist yet or is owned by $this->owner.

Parameters

string $key

The key of the data to store.

mixed $value

The data to store.

Return Value

bool

TRUE if the data was set, or FALSE if it already exists and is not owned by $this->user.

Exceptions

TempStoreException

set(string $key, mixed $value)

Stores a particular key/value pair in this SharedTempStore.

Parameters

string $key

The key of the data to store.

mixed $value

The data to store.

Exceptions

TempStoreException

Lock|null getMetadata(string $key)

Returns the metadata associated with a particular key/value pair.

Parameters

string $key

The key of the data to store.

Return Value

Lock|null

An object with the owner and updated time if the key has a value, or NULL otherwise.

delete(string $key)

Deletes data from the store for a given key and releases the lock on it.

Parameters

string $key

The key of the data to delete.

Exceptions

TempStoreException

bool deleteIfOwner(string $key)

Deletes data from the store for a given key and releases the lock on it.

Only delete the given key if it is owned by $this->owner.

Parameters

string $key

The key of the data to delete.

Return Value

bool

TRUE if the object was deleted or does not exist, FALSE if it exists but is not owned by $this->owner.

Exceptions

TempStoreException

protected ensureAnonymousSession()

Stores the owner in the session if the user is anonymous.

This method should be called when a value is set.