Skip to content

Commit f45dba1

Browse files
committed
minor #14231 [Lock] Add documentation about Shared Locks (jderusse)
This PR was merged into the master branch. Discussion ---------- [Lock] Add documentation about Shared Locks Fixes #14225 implemented in symfony/symfony#37752 Commits ------- dfd97c5 Add documentation about read/write locks
2 parents 1b5f330 + dfd97c5 commit f45dba1

File tree

1 file changed

+61
-11
lines changed

1 file changed

+61
-11
lines changed

components/lock.rst

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,56 @@ This component also provides two useful methods related to expiring locks:
165165
``getExpiringDate()`` (which returns ``null`` or a ``\DateTimeImmutable``
166166
object) and ``isExpired()`` (which returns a boolean).
167167

168+
Shared Locks
169+
------------
170+
171+
Sometimes, a data structure cannot be updated atomically and is invalid during
172+
the time of the update. In this situation, other process should not read or
173+
write the data until the update is complete. But once updated, multiple process
174+
can read the data in parallel.
175+
176+
In this situation, a common solution is to use shared lock which allows
177+
concurent access for read-only operations, while write operations require
178+
exclusive access.
179+
180+
Use the :method:`Symfony\\Component\\Lock\\LockInterface::acquireRead` method
181+
to acquire a read-only lock, and the existing
182+
:method:`Symfony\\Component\\Lock\\LockInterface::acquire` method to acquire a
183+
write lock.::
184+
185+
$lock = $factory->createLock('user'.$user->id);
186+
if (!$lock->acquireRead()) {
187+
return;
188+
}
189+
190+
Similare to the ``acquire`` method, pass ``true`` as the argument of the ``acquireRead()``
191+
method to acquire the lock in a blocking mode.::
192+
193+
$lock = $factory->createLock('user'.$user->id);
194+
$lock->acquireRead(true);
195+
196+
When a read-only lock is acquired with the method ``acquireRead``, it's
197+
possible to **Promote** the lock, and change it to write lock, by calling the
198+
``acquire`` method.::
199+
200+
$lock = $factory->createLock('user'.$userId);
201+
$lock->acquireRead(true);
202+
203+
if (!$this->shouldUpdate($userId)) {
204+
return;
205+
}
206+
207+
$lock->acquire(true); // Promote the lock to write lock
208+
$this->update($userId);
209+
210+
In the same way, it's possible to **Demote** a write lock, and change it to a
211+
read-only lock by calling the ``acquireRead`` method.
212+
213+
.. versionadded:: 5.2
214+
215+
The ``Lock::acquireRead`` method and ``SharedLockStoreInterface`` interface
216+
and were introduced in Symfony 5.2.
217+
168218
The Owner of The Lock
169219
---------------------
170220

@@ -219,17 +269,17 @@ Locks are created and managed in ``Stores``, which are classes that implement
219269

220270
The component includes the following built-in store types:
221271

222-
============================================ ====== ======== ========
223-
Store Scope Blocking Expiring
224-
============================================ ====== ======== ========
225-
:ref:`FlockStore <lock-store-flock>` local yes no
226-
:ref:`MemcachedStore <lock-store-memcached>` remote no yes
227-
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes
228-
:ref:`PdoStore <lock-store-pdo>` remote no yes
229-
:ref:`RedisStore <lock-store-redis>` remote no yes
230-
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no
231-
:ref:`ZookeeperStore <lock-store-zookeeper>` remote no no
232-
============================================ ====== ======== ========
272+
============================================ ====== ======== ======== =======
273+
Store Scope Blocking Expiring Sharing
274+
============================================ ====== ======== ======== =======
275+
:ref:`FlockStore <lock-store-flock>` local yes no yes
276+
:ref:`MemcachedStore <lock-store-memcached>` remote no yes no
277+
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes no
278+
:ref:`PdoStore <lock-store-pdo>` remote no yes no
279+
:ref:`RedisStore <lock-store-redis>` remote no yes yes
280+
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no no
281+
:ref:`ZookeeperStore <lock-store-zookeeper>` remote no no no
282+
============================================ ====== ======== ======== =======
233283

234284
.. _lock-store-flock:
235285

0 commit comments

Comments
 (0)