Skip to content

Commit 6b5be0c

Browse files
committed
Add documentation about serializing lock
1 parent eab61bb commit 6b5be0c

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

components/lock.rst

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ method can be safely called repeatedly, even if the lock is already acquired.
6868
across several requests. To disable the automatic release behavior, set the
6969
third argument of the ``createLock()`` method to ``false``.
7070

71+
Serializing Locks
72+
------------------
73+
74+
The ``Key`` contains the state of the ``Lock`` and can be serialized. This
75+
allows the user to begin a long job in a process by acquiring the lock, and
76+
to continue the job in an other process within the same lock.::
77+
78+
use Symfony\Component\Lock\Key;
79+
use Symfony\Component\Lock\Lock;
80+
81+
$key = new Key('article.'.$article->getId());
82+
$lock = new Lock($key, $this->store, 300, false);
83+
$lock->acquire(true);
84+
85+
$this->bus->dispatch(new RefreshTaxonomy($article, $key));
86+
87+
.. note::
88+
89+
Don't forget to disable the autoRelease to avoid releasing the lock when
90+
the destructor will be called.
91+
92+
All stores are not compatible with serialization and cross-process locking:
93+
For instance the kernel will automatically releases Semaphores acquires by the
94+
:ref:`SemaphoreStore <lock-store-semaphore>` store.
95+
Wen a lock is acquired with such store, and hte application try to serialize
96+
the key, and exception will be thrown
97+
7198
.. _lock-blocking-locks:
7299
73100
Blocking Locks
@@ -286,17 +313,17 @@ Locks are created and managed in ``Stores``, which are classes that implement
286313

287314
The component includes the following built-in store types:
288315

289-
============================================ ====== ======== ======== =======
290-
Store Scope Blocking Expiring Sharing
291-
============================================ ====== ======== ======== =======
292-
:ref:`FlockStore <lock-store-flock>` local yes no yes
293-
:ref:`MemcachedStore <lock-store-memcached>` remote no yes no
294-
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes no
295-
:ref:`PdoStore <lock-store-pdo>` remote no yes no
296-
:ref:`RedisStore <lock-store-redis>` remote no yes yes
297-
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no no
298-
:ref:`ZookeeperStore <lock-store-zookeeper>` remote no no no
299-
============================================ ====== ======== ======== =======
316+
============================================ ====== ========= ======== ======== =======
317+
Store Scope Serialize Blocking Expiring Sharing
318+
============================================ ====== ========= ======== ======== =======
319+
:ref:`FlockStore <lock-store-flock>` local no yes no yes
320+
:ref:`MemcachedStore <lock-store-memcached>` remote yes no yes no
321+
:ref:`MongoDbStore <lock-store-mongodb>` remote yes no yes no
322+
:ref:`PdoStore <lock-store-pdo>` remote yes no yes no
323+
:ref:`RedisStore <lock-store-redis>` remote yes no yes yes
324+
:ref:`SemaphoreStore <lock-store-semaphore>` local no yes no no
325+
:ref:`ZookeeperStore <lock-store-zookeeper>` remote no no no no
326+
============================================ ====== ========= ======== ======== =======
300327

301328
.. _lock-store-flock:
302329

0 commit comments

Comments
 (0)