26
26
-----
27
27
28
28
In 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 ``.
30
31
31
32
The :method: `Symfony\\ Component\\ Lock\\ LockInterface::acquire ` method tries to
32
33
acquire the lock. If the lock can not be acquired, the method throws a
36
37
37
38
.. code-block :: php
38
39
40
+ use Symfony\Component\Lock\Factory;
39
41
use Symfony\Component\Lock\Store\SemaphoreStore;
40
42
use Symfony\Component\Lock\Exception\LockConflictedException;
41
43
42
44
$store = new SemaphoreStore();
43
- $lock = $store->createLock('invoice-pdf-generation');
45
+ $factory = new Factory($store);
46
+ $lock = $factory->createLock('invoice-pdf-generation');
44
47
45
48
try {
46
49
$lock->acquire();
@@ -62,7 +65,7 @@ The first argument of ``createLock`` is a string representation of the
62
65
distinguishes locks instances, even when they are created from the same
63
66
``resource ``.
64
67
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.
68
+ instance of Lock returned by the ``Factory ::createLock `` method.
66
69
67
70
Blocking locks
68
71
--------------
@@ -77,13 +80,16 @@ you can decorate it with the ``RetryTillSaveStore``.
77
80
78
81
.. code-block :: php
79
82
83
+ use Symfony\Component\Lock\Factory;
80
84
use Symfony\Component\Lock\Store\RedisStore;
81
85
use Symfony\Component\Lock\Store\RetryTillSaveStore;
82
86
83
87
$store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
84
88
$store = new RetryTillSaveStore($store);
85
89
86
- $lock = $store->createLock('notification-flush');
90
+ $factory = new Factory($store);
91
+
92
+ $lock = $factory->createLock('notification-flush');
87
93
88
94
$lock->acquire(true);
89
95
@@ -110,17 +116,19 @@ the ``resource`` will stay lock till the timeout.
110
116
111
117
.. code-block :: php
112
118
119
+ use Symfony\Component\Lock\Factory;
113
120
use Symfony\Component\Lock\Store\RedisStore;
114
121
115
122
$store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
116
123
117
- $lock = $store->createLock('charts-generation', 30);
124
+ $factory = new Factory($store);
125
+ $lock = $factory->createLock('charts-generation', 30);
118
126
119
127
$lock->acquire();
120
128
try {
121
129
// perfom a job during less than 30 seconds
122
130
} finally {
123
- $lock->release()
131
+ $lock->release();
124
132
}
125
133
126
134
.. tip ::
@@ -136,11 +144,13 @@ regularly refresh the lock
136
144
137
145
.. code-block :: php
138
146
147
+ use Symfony\Component\Lock\Factory;
139
148
use Symfony\Component\Lock\Store\RedisStore;
140
149
141
150
$store = new RedisStore(new \Predis\Client('tcp://localhost:6379'));
142
151
143
- $lock = $store->createLock('charts-generation', 30);
152
+ $factory = new Factory($store);
153
+ $lock = $factory->createLock('charts-generation', 30);
144
154
145
155
$lock->acquire();
146
156
try {
@@ -151,7 +161,7 @@ regularly refresh the lock
151
161
// resource is locked for 30 more seconds.
152
162
}
153
163
} finally {
154
- $lock->release()
164
+ $lock->release();
155
165
}
156
166
157
167
Available Stores
@@ -219,17 +229,18 @@ The MemcachedStore stores state of ``resource`` in a Memcached server. This
219
229
Memcached does not supports TTL lower than 1 seconds.
220
230
221
231
It requires to have installed Memcached and have created a connection that
222
- implements the ``\Memcached `` classes::
232
+ implements the ``\Memcached `` class.
223
233
224
234
.. code-block :: php
225
235
226
- use Symfony\Component\Lock\Store\RedisStore ;
236
+ use Symfony\Component\Lock\Store\MemcachedStore ;
227
237
228
238
$memcached = new \Memcached();
229
239
$memcached->addServer('localhost', 11211);
230
240
231
241
$store = new MemcachedStore($memcached);
232
242
243
+
233
244
.. _lock-store-redis :
234
245
235
246
RedisStore
@@ -241,7 +252,7 @@ locks.
241
252
242
253
It requires to have installed Redis and have created a connection that
243
254
implements the ``\Redis ``, ``\RedisArray ``, ``\RedisCluster `` or ``\Predis ``
244
- classes::
255
+ classes
245
256
246
257
.. code-block :: php
247
258
@@ -263,7 +274,7 @@ The SemaphoreStore uses the PHP semaphore functions to lock a ``resources``.
263
274
264
275
use Symfony\Component\Lock\Store\SemaphoreStore;
265
276
266
- $store = new SemaphoreStore($redis );
277
+ $store = new SemaphoreStore();
267
278
268
279
.. _lock-store-combined :
269
280
0 commit comments