5 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
6 use Drupal\Core\Lock\LockBackendInterface;
7 use Symfony\Component\HttpFoundation\RequestStack;
10 * Stores and retrieves temporary data for a given owner.
12 * A SharedTempStore can be used to make temporary, non-cache data available
13 * across requests. The data for the SharedTempStore is stored in one key/value
14 * collection. SharedTempStore data expires automatically after a given
17 * The SharedTempStore is different from a cache, because the data in it is not
18 * yet saved permanently and so it cannot be rebuilt. Typically, the
19 * SharedTempStore might be used to store work in progress that is later saved
20 * permanently elsewhere, e.g. autosave data, multistep forms, or in-progress
21 * changes to complex configuration that are not ready to be saved.
23 * Each SharedTempStore belongs to a particular owner (e.g. a user, session, or
24 * process). Multiple owners may use the same key/value collection, and the
25 * owner is stored along with the key/value pair.
27 * Every key is unique within the collection, so the SharedTempStore can check
28 * whether a particular key is already set by a different owner. This is
29 * useful for informing one owner that the data is already in use by another;
30 * for example, to let one user know that another user is in the process of
31 * editing certain data, or even to restrict other users from editing it at
32 * the same time. It is the responsibility of the implementation to decide
33 * when and whether one owner can use or update another owner's data.
35 * If you want to be able to ensure that the data belongs to the current user,
36 * use \Drupal\user\PrivateTempStore.
38 class SharedTempStore {
41 * The key/value storage object used for this data.
43 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
48 * The lock object used for this data.
50 * @var \Drupal\Core\Lock\LockBackendInterface
52 protected $lockBackend;
57 * @var \Symfony\Component\HttpFoundation\RequestStack
59 protected $requestStack;
62 * The owner key to store along with the data (e.g. a user or session ID).
69 * The time to live for items in seconds.
71 * By default, data is stored for one week (604800 seconds) before expiring.
78 * Constructs a new object for accessing data from a key/value store.
80 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $storage
81 * The key/value storage object used for this data. Each storage object
82 * represents a particular collection of data and will contain any number
84 * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
85 * The lock object used for this data.
87 * The owner key to store along with the data (e.g. a user or session ID).
88 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
91 * The time to live for items, in seconds.
93 public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, $owner, RequestStack $request_stack, $expire = 604800) {
94 $this->storage = $storage;
95 $this->lockBackend = $lock_backend;
96 $this->owner = $owner;
97 $this->requestStack = $request_stack;
98 $this->expire = $expire;
102 * Retrieves a value from this SharedTempStore for a given key.
105 * The key of the data to retrieve.
108 * The data associated with the key, or NULL if the key does not exist.
110 public function get($key) {
111 if ($object = $this->storage->get($key)) {
112 return $object->data;
117 * Retrieves a value from this SharedTempStore for a given key.
119 * Only returns the value if the value is owned by $this->owner.
122 * The key of the data to retrieve.
125 * The data associated with the key, or NULL if the key does not exist.
127 public function getIfOwner($key) {
128 if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) {
129 return $object->data;
134 * Stores a particular key/value pair only if the key doesn't already exist.
137 * The key of the data to check and store.
138 * @param mixed $value
142 * TRUE if the data was set, or FALSE if it already existed.
144 public function setIfNotExists($key, $value) {
146 'owner' => $this->owner,
148 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
150 return $this->storage->setWithExpireIfNotExists($key, $value, $this->expire);
154 * Stores a particular key/value pair in this SharedTempStore.
156 * Only stores the given key/value pair if it does not exist yet or is owned
160 * The key of the data to store.
161 * @param mixed $value
165 * TRUE if the data was set, or FALSE if it already exists and is not owned
168 * @throws \Drupal\user\TempStoreException
169 * Thrown when a lock for the backend storage could not be acquired.
171 public function setIfOwner($key, $value) {
172 if ($this->setIfNotExists($key, $value)) {
176 if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) {
177 $this->set($key, $value);
185 * Stores a particular key/value pair in this SharedTempStore.
188 * The key of the data to store.
189 * @param mixed $value
192 * @throws \Drupal\user\TempStoreException
193 * Thrown when a lock for the backend storage could not be acquired.
195 public function set($key, $value) {
196 if (!$this->lockBackend->acquire($key)) {
197 $this->lockBackend->wait($key);
198 if (!$this->lockBackend->acquire($key)) {
199 throw new TempStoreException("Couldn't acquire lock to update item '$key' in '{$this->storage->getCollectionName()}' temporary storage.");
204 'owner' => $this->owner,
206 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
208 $this->storage->setWithExpire($key, $value, $this->expire);
209 $this->lockBackend->release($key);
213 * Returns the metadata associated with a particular key/value pair.
216 * The key of the data to store.
219 * An object with the owner and updated time if the key has a value, or
222 public function getMetadata($key) {
223 // Fetch the key/value pair and its metadata.
224 $object = $this->storage->get($key);
226 // Don't keep the data itself in memory.
227 unset($object->data);
233 * Deletes data from the store for a given key and releases the lock on it.
236 * The key of the data to delete.
238 * @throws \Drupal\user\TempStoreException
239 * Thrown when a lock for the backend storage could not be acquired.
241 public function delete($key) {
242 if (!$this->lockBackend->acquire($key)) {
243 $this->lockBackend->wait($key);
244 if (!$this->lockBackend->acquire($key)) {
245 throw new TempStoreException("Couldn't acquire lock to delete item '$key' from {$this->storage->getCollectionName()} temporary storage.");
248 $this->storage->delete($key);
249 $this->lockBackend->release($key);
253 * Deletes data from the store for a given key and releases the lock on it.
255 * Only delete the given key if it is owned by $this->owner.
258 * The key of the data to delete.
261 * TRUE if the object was deleted or does not exist, FALSE if it exists but
262 * is not owned by $this->owner.
264 * @throws \Drupal\user\TempStoreException
265 * Thrown when a lock for the backend storage could not be acquired.
267 public function deleteIfOwner($key) {
268 if (!$object = $this->storage->get($key)) {
271 elseif ($object->owner == $this->owner) {