33 single: Components; Lock
44
55The Lock Component
6- ====================
6+ ==================
77
88 The Lock Component provides a mechanism to guarantee an exclusive access
99 into a critical section. The component ships with ready to use stores for
@@ -26,31 +26,29 @@ Usage
2626-----
2727
2828In order to centralize state of locks, you first need to create a ``Store ``.
29- Then, you can ask to this store to create a Lock for your ``resource ``.
29+ Then, you can use the :class: `Symfony\\ Component\\ Lock\\ Factory ` to create a
30+ Lock for your ``resource ``.
3031
3132The :method: `Symfony\\ Component\\ Lock\\ LockInterface::acquire ` method tries to
32- acquire the lock. If the lock can not be acquired, the method throws a
33- :class: `Symfony\\ Component\\ Lock\\ Exception\\ LockConflictedException `. You can
34- safely call the ``acquire() `` method several times, even if you already acquired
35- it.
33+ acquire the lock. If the lock can not be acquired, the method returns ``false ``.
34+ You can safely call the ``acquire() `` method several times, even if you already
35+ acquired it.
3636
3737.. code-block :: php
3838
39+ use Symfony\Component\Lock\Factory;
3940 use Symfony\Component\Lock\Store\SemaphoreStore;
40- use Symfony\Component\Lock\Exception\LockConflictedException;
4141
4242 $store = new SemaphoreStore();
43- $lock = $store->createLock('invoice-pdf-generation');
43+ $factory = new Factory($store);
44+ $lock = $factory->createLock('invoice-pdf-generation');
4445
45- try {
46- $lock->acquire();
46+ if ($lock->acquire()) {
4747 // the resource "invoice-pdf-generation" is locked.
4848
4949 // You can compute and generate invoice safely here.
5050
5151 $lock->release();
52- } catch (LockConflictedException $e) {
53- // the resource "invoice-pdf-generation" is already locked by another process
5452 }
5553
5654 The first argument of ``createLock `` is a string representation of the
@@ -62,9 +60,9 @@ The first argument of ``createLock`` is a string representation of the
6260 distinguishes locks instances, even when they are created from the same
6361 ``resource ``.
6462 If you want to share a lock in several services. You have to share the
65- instance of Lock returned by the ``Store ::createLock `` method.
63+ instance of Lock returned by the ``Factory ::createLock `` method.
6664
67- Blocking locks
65+ Blocking Locks
6866--------------
6967
7068You can pass an optional blocking argument as the first argument to the
@@ -77,70 +75,76 @@ you can decorate it with the ``RetryTillSaveStore``.
7775
7876.. code-block :: php
7977
78+ use Symfony\Component\Lock\Factory;
8079 use Symfony\Component\Lock\Store\RedisStore;
8180 use Symfony\Component\Lock\Store\RetryTillSaveStore;
8281
8382 $store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
8483 $store = new RetryTillSaveStore($store);
8584
86- $lock = $store->createLock('notification-flush');
85+ $factory = new Factory($store);
86+
87+ $lock = $factory->createLock('notification-flush');
8788
8889 $lock->acquire(true);
8990
90- Expirable Locks
91- ---------------
91+ Expiring Locks
92+ --------------
9293
93- Working with a remote ``Store `` is hard. There is now way for the remote
94- ``Store `` to know if the locker process is till alive.
94+ Working with a remote ``Store `` is hard. There is no way for the remote
95+ ``Store `` to know if the locker process is still alive.
9596Due to bugs, fatal errors or segmentation fault, we can't guarantee that the
9697``release() `` method will be called, which would cause a ``resource `` to be locked
9798infinitely.
9899
99- To fill this gap, the remote ``Stores `` provide an expirable mechanism: The lock
100- is acquired for a defined amount of time (named TTL for Time To Live).
101- When the timeout occurred , the lock is automatically released even if the locker
100+ To fill this gap, the remote ``Store `` provide an expiration mechanism: The
101+ lock is acquired for a defined amount of time (named TTL for Time To Live).
102+ When the timeout occurs , the lock is automatically released even if the locker
102103don't call the ``release() `` method.
103104
104- That's why, when you create a lock on an expirable ``Store ``. You have to choose
105- carefully the correct TTL. When too low, you take the risk to "lose " the lock
105+ That's why, when you create a lock on an expiring ``Store ``, you have to choose
106+ carefully the correct TTL. When too low, you take the risk to "loose " the lock
106107(and someone else acquire it) whereas you don't finish your task.
107108When too high and the process crash before you call the ``release() `` method,
108109the ``resource `` will stay lock till the timeout.
109110
110-
111111.. code-block :: php
112112
113+ use Symfony\Component\Lock\Factory;
113114 use Symfony\Component\Lock\Store\RedisStore;
114115
115116 $store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
116117
117- $lock = $store->createLock('charts-generation', 30);
118+ $factory = new Factory($store);
119+ $lock = $factory->createLock('charts-generation', 30);
118120
119121 $lock->acquire();
120122 try {
121123 // perfom a job during less than 30 seconds
122124 } finally {
123- $lock->release()
125+ $lock->release();
124126 }
125127
126128 .. tip ::
127129
128130 To avoid letting the Lock in a locking state, try to always release an
129- expirable lock by wrapping the job in a try/catch block for instance.
131+ expiring lock by wrapping the job in a try/catch block for instance.
130132
131133When you have to work on a really long task, you should not set the TTL to
132- overlap the duration of this task. Instead, the Lock Component expose a
134+ overlap the duration of this task. Instead, the Lock Component exposes a
133135:method: `Symfony\\ Component\\ Lock\\ LockInterface::refresh ` method in order to
134136put off the TTL of the Lock. Thereby you can choose a small initial TTL, and
135- regularly refresh the lock
137+ regularly refresh the lock.
136138
137139.. code-block :: php
138140
141+ use Symfony\Component\Lock\Factory;
139142 use Symfony\Component\Lock\Store\RedisStore;
140143
141144 $store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
142145
143- $lock = $store->createLock('charts-generation', 30);
146+ $factory = new Factory($store);
147+ $lock = $factory->createLock('charts-generation', 30);
144148
145149 $lock->acquire();
146150 try {
@@ -151,7 +155,7 @@ regularly refresh the lock
151155 // resource is locked for 30 more seconds.
152156 }
153157 } finally {
154- $lock->release()
158+ $lock->release();
155159 }
156160
157161 Available Stores
@@ -162,31 +166,31 @@ This component provides several adapters ready to use in your applications.
162166
163167Here is a small summary of available ``Stores `` and their capabilities.
164168
165- +----------------------------------------------+--------+----------+----------- +
166- | Store | Scope | Blocking | Expirable |
167- +==============================================+========+==========+=========== +
168- | :ref: `FlockStore <lock-store-flock >` | local | yes | no |
169- +----------------------------------------------+--------+----------+----------- +
170- | :ref: `MemcachedStore <lock-store-memcached >` | remote | no | yes |
171- +----------------------------------------------+--------+----------+----------- +
172- | :ref: `RedisStore <lock-store-redis >` | remote | no | yes |
173- +----------------------------------------------+--------+----------+----------- +
174- | :ref: `SemaphoreStore <lock-store-semaphore >` | local | yes | no |
175- +----------------------------------------------+--------+----------+----------- +
169+ +----------------------------------------------+--------+----------+----------+
170+ | Store | Scope | Blocking | Expiring |
171+ +==============================================+========+==========+==========+
172+ | :ref: `FlockStore <lock-store-flock >` | local | yes | no |
173+ +----------------------------------------------+--------+----------+----------+
174+ | :ref: `MemcachedStore <lock-store-memcached >` | remote | no | yes |
175+ +----------------------------------------------+--------+----------+----------+
176+ | :ref: `RedisStore <lock-store-redis >` | remote | no | yes |
177+ +----------------------------------------------+--------+----------+----------+
178+ | :ref: `SemaphoreStore <lock-store-semaphore >` | local | yes | no |
179+ +----------------------------------------------+--------+----------+----------+
176180
177181.. tip ::
178182
179183 Calling the :method: `Symfony\\ Component\\ Lock\\ LockInterface::refresh `
180- method on a Lock created from a non expirable ``Store `` like
184+ method on a Lock created from a non expiring ``Store `` like
181185 :ref: `FlockStore <lock-store-flock >` will do nothing.
182186
183187.. _lock-store-flock :
184188
185189FlockStore
186190~~~~~~~~~~
187191
188- The FlockStore use the fileSystem on the local computer to lock and store the
189- ``resource ``. It does not supports expiration, but the lock is automatically
192+ The FlockStore uses the fileSystem on the local computer to lock and store the
193+ ``resource ``. It does not support expiration, but the lock is automatically
190194released when the PHP process is terminated.
191195
192196.. code-block :: php
@@ -200,7 +204,7 @@ file will be created.
200204
201205.. caution ::
202206
203- Beware, some filesystem (like some version of NFS) does not support locking.
207+ Beware, some filesystems (like some version of NFS) do not support locking.
204208 We suggest to use local file, or to use a Store dedicated to remote usage
205209 like Redis or Memcached.
206210
@@ -212,18 +216,18 @@ MemcachedStore
212216~~~~~~~~~~~~~~
213217
214218The MemcachedStore stores state of ``resource `` in a Memcached server. This
215- ``Store `` does not support blocking, and expect a TLL to avoid infinity locks.
219+ ``Store `` does not support blocking, and expects a TLL to avoid infinity locks.
216220
217221.. note ::
218222
219- Memcached does not supports TTL lower than 1 seconds .
223+ Memcached does not support TTL lower than 1 second .
220224
221225It requires to have installed Memcached and have created a connection that
222- implements the ``\Memcached `` classes::
226+ implements the ``\Memcached `` class.
223227
224228.. code-block :: php
225229
226- use Symfony\Component\Lock\Store\RedisStore ;
230+ use Symfony\Component\Lock\Store\MemcachedStore ;
227231
228232 $memcached = new \Memcached();
229233 $memcached->addServer('localhost', 11211);
@@ -241,7 +245,7 @@ locks.
241245
242246It requires to have installed Redis and have created a connection that
243247implements the ``\Redis ``, ``\RedisArray ``, ``\RedisCluster `` or ``\Predis ``
244- classes::
248+ classes
245249
246250.. code-block :: php
247251
@@ -257,13 +261,13 @@ classes::
257261SemaphoreStore
258262~~~~~~~~~~~~~~
259263
260- The SemaphoreStore uses the PHP semaphore function to lock a ``resources ``.
264+ The SemaphoreStore uses the PHP semaphore functions to lock a ``resources ``.
261265
262266.. code-block :: php
263267
264268 use Symfony\Component\Lock\Store\SemaphoreStore;
265269
266- $store = new SemaphoreStore($redis );
270+ $store = new SemaphoreStore();
267271
268272 .. _lock-store-combined :
269273
0 commit comments