5 use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
6 use Drupal\Core\Lock\LockBackendInterface;
7 use Drupal\Core\Session\AccountProxyInterface;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * Stores and retrieves temporary data for a given owner.
13 * A PrivateTempStore can be used to make temporary, non-cache data available
14 * across requests. The data for the PrivateTempStore is stored in one
15 * key/value collection. PrivateTempStore data expires automatically after a
18 * The PrivateTempStore is different from a cache, because the data in it is not
19 * yet saved permanently and so it cannot be rebuilt. Typically, the
20 * PrivateTempStore might be used to store work in progress that is later saved
21 * permanently elsewhere, e.g. autosave data, multistep forms, or in-progress
22 * changes to complex configuration that are not ready to be saved.
24 * The PrivateTempStore differs from the SharedTempStore in that all keys are
25 * ensured to be unique for a particular user and users can never share data. If
26 * you want to be able to share data between users or use it for locking, use
27 * \Drupal\user\SharedTempStore.
29 class PrivateTempStore {
32 * The key/value storage object used for this data.
34 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
39 * The lock object used for this data.
41 * @var \Drupal\Core\Lock\LockBackendInterface
43 protected $lockBackend;
48 * @var \Drupal\Core\Session\AccountProxyInterface
50 protected $currentUser;
55 * @var \Symfony\Component\HttpFoundation\RequestStack
57 protected $requestStack;
60 * The time to live for items in seconds.
62 * By default, data is stored for one week (604800 seconds) before expiring.
69 * Constructs a new object for accessing data from a key/value store.
71 * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $storage
72 * The key/value storage object used for this data. Each storage object
73 * represents a particular collection of data and will contain any number
75 * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
76 * The lock object used for this data.
77 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
78 * The current user account.
79 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
82 * The time to live for items, in seconds.
84 public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, AccountProxyInterface $current_user, RequestStack $request_stack, $expire = 604800) {
85 $this->storage = $storage;
86 $this->lockBackend = $lock_backend;
87 $this->currentUser = $current_user;
88 $this->requestStack = $request_stack;
89 $this->expire = $expire;
93 * Retrieves a value from this PrivateTempStore for a given key.
96 * The key of the data to retrieve.
99 * The data associated with the key, or NULL if the key does not exist.
101 public function get($key) {
102 $key = $this->createkey($key);
103 if (($object = $this->storage->get($key)) && ($object->owner == $this->getOwner())) {
104 return $object->data;
109 * Stores a particular key/value pair in this PrivateTempStore.
112 * The key of the data to store.
113 * @param mixed $value
116 * @throws \Drupal\user\TempStoreException
117 * Thrown when a lock for the backend storage could not be acquired.
119 public function set($key, $value) {
120 $key = $this->createkey($key);
121 if (!$this->lockBackend->acquire($key)) {
122 $this->lockBackend->wait($key);
123 if (!$this->lockBackend->acquire($key)) {
124 throw new TempStoreException("Couldn't acquire lock to update item '$key' in '{$this->storage->getCollectionName()}' temporary storage.");
129 'owner' => $this->getOwner(),
131 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
133 $this->storage->setWithExpire($key, $value, $this->expire);
134 $this->lockBackend->release($key);
138 * Returns the metadata associated with a particular key/value pair.
141 * The key of the data to store.
144 * An object with the owner and updated time if the key has a value, or
147 public function getMetadata($key) {
148 $key = $this->createkey($key);
149 // Fetch the key/value pair and its metadata.
150 $object = $this->storage->get($key);
152 // Don't keep the data itself in memory.
153 unset($object->data);
159 * Deletes data from the store for a given key and releases the lock on it.
162 * The key of the data to delete.
165 * TRUE if the object was deleted or does not exist, FALSE if it exists but
166 * is not owned by $this->owner.
168 * @throws \Drupal\user\TempStoreException
169 * Thrown when a lock for the backend storage could not be acquired.
171 public function delete($key) {
172 $key = $this->createkey($key);
173 if (!$object = $this->storage->get($key)) {
176 elseif ($object->owner != $this->getOwner()) {
179 if (!$this->lockBackend->acquire($key)) {
180 $this->lockBackend->wait($key);
181 if (!$this->lockBackend->acquire($key)) {
182 throw new TempStoreException("Couldn't acquire lock to delete item '$key' from '{$this->storage->getCollectionName()}' temporary storage.");
185 $this->storage->delete($key);
186 $this->lockBackend->release($key);
191 * Ensures that the key is unique for a user.
197 * The unique key for the user.
199 protected function createkey($key) {
200 return $this->getOwner() . ':' . $key;
204 * Gets the current owner based on the current user or the session ID.
209 protected function getOwner() {
210 return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId();