@@ -22,11 +22,14 @@ php-lock/lock follows semantic versioning. Read more on [semver.org][1].
22
22
23
23
- PHP 7.1 or above
24
24
- Optionally [ nrk/predis] [ 2 ] to use the Predis locks.
25
- - Optionally the [ php-pcntl] [ 3 ] extension to enable locking with ` flock() ` without
26
- busy waiting in CLI scripts.
27
- - Optionally ` flock() ` , ` ext-redis ` , ` ext-pdo_mysql ` , ` ext-pdo_sqlite ` , ` ext-pdo_pgsql ` or ` ext-memcached ` can be used as a backend for locks. See examples below.
28
- - If ` ext-redis ` is used for locking and is configured to use igbinary for serialization or
29
- lzf for compression, additionally ` ext-igbinary ` and/or ` ext-lzf ` have to be installed.
25
+ - Optionally the [ php-pcntl] [ 3 ] extension to enable locking with ` flock() `
26
+ without busy waiting in CLI scripts.
27
+ - Optionally ` flock() ` , ` ext-redis ` , ` ext-pdo_mysql ` , ` ext-pdo_sqlite ` ,
28
+ ` ext-pdo_pgsql ` or ` ext-memcached ` can be used as a backend for locks. See
29
+ examples below.
30
+ - If ` ext-redis ` is used for locking and is configured to use igbinary for
31
+ serialization or lzf for compression, additionally ` ext-igbinary ` and/or
32
+ ` ext-lzf ` have to be installed.
30
33
31
34
----
32
35
@@ -64,7 +67,10 @@ return value `false` or `null` should be seen as a failed action.
64
67
Example:
65
68
66
69
``` php
67
- $newBalance = $mutex->synchronized(function () use ($bankAccount, $amount): int {
70
+ $newBalance = $mutex->synchronized(function () use (
71
+ $bankAccount,
72
+ $amount
73
+ ): int {
68
74
$balance = $bankAccount->getBalance();
69
75
$balance -= $amount;
70
76
if ($balance < 0) {
@@ -133,26 +139,26 @@ Example:
133
139
``` php
134
140
try {
135
141
// or $mutex->check(...)
136
- $mutex->synchronized(function () {
142
+ $result = $ mutex->synchronized(function () {
137
143
if (someCondition()) {
138
144
throw new \DomainException();
139
145
}
140
146
141
147
return "result";
142
148
});
143
- } catch (LockReleaseException $unlock_exception ) {
144
- if ($unlock_exception ->getCodeException() !== null) {
145
- $code_exception = $unlock_exception ->getCodeException()
149
+ } catch (LockReleaseException $unlockException ) {
150
+ if ($unlockException ->getCodeException() !== null) {
151
+ $codeException = $unlockException ->getCodeException()
146
152
// do something with the code exception
147
153
} else {
148
- $code_result = $unlock_exception ->getCodeResult();
154
+ $code_result = $unlockException ->getCodeResult();
149
155
// do something with the code result
150
156
}
151
-
157
+
152
158
// deal with LockReleaseException or propagate it
153
- throw $unlock_exception ;
159
+ throw $unlockException ;
154
160
}
155
- ```
161
+ ```
156
162
157
163
### Implementations
158
164
@@ -189,15 +195,15 @@ $mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
189
195
$balance -= $amount;
190
196
if (!$memcached->cas($casToken, "balance", $balance)) {
191
197
return;
192
-
193
198
}
194
199
$mutex->notify();
195
200
});
196
201
```
197
202
198
203
#### FlockMutex
199
204
200
- The ** FlockMutex** is a lock implementation based on [ ` flock() ` ] ( http://php.net/manual/en/function.flock.php ) .
205
+ The ** FlockMutex** is a lock implementation based on
206
+ [ ` flock() ` ] ( http://php.net/manual/en/function.flock.php ) .
201
207
202
208
Example:
203
209
``` php
@@ -207,19 +213,18 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
207
213
$balance -= $amount;
208
214
if ($balance < 0) {
209
215
throw new \DomainException("You have no credit.");
210
-
211
216
}
212
217
$bankAccount->setBalance($balance);
213
218
});
214
219
```
215
220
216
221
Timeouts are supported as an optional second argument. This uses the ` ext-pcntl `
217
- extension if possible or busy waiting if not.
222
+ extension if possible or busy waiting if not.
218
223
219
224
#### MemcachedMutex
220
225
221
- The ** MemcachedMutex**
222
- is a spinlock implementation which uses the [ ` Memcached ` API] ( http://php.net/manual/en/book.memcached.php ) .
226
+ The ** MemcachedMutex** is a spinlock implementation which uses the
227
+ [ ` Memcached ` API] ( http://php.net/manual/en/book.memcached.php ) .
223
228
224
229
Example:
225
230
``` php
@@ -232,21 +237,21 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
232
237
$balance -= $amount;
233
238
if ($balance < 0) {
234
239
throw new \DomainException("You have no credit.");
235
-
236
240
}
237
241
$bankAccount->setBalance($balance);
238
242
});
239
243
```
240
244
241
245
#### PHPRedisMutex
242
246
243
- The ** PHPRedisMutex** is the distributed lock implementation of [ RedLock] ( http://redis.io/topics/distlock )
244
- which uses the [ ` phpredis ` extension] ( https://github.com/phpredis/phpredis ) .
247
+ The ** PHPRedisMutex** is the distributed lock implementation of
248
+ [ RedLock] ( http://redis.io/topics/distlock ) which uses the
249
+ [ ` phpredis ` extension] ( https://github.com/phpredis/phpredis ) .
245
250
246
251
This implementation requires at least ` phpredis-2.2.4 ` .
247
252
248
- If used with a cluster of Redis servers, acquiring and releasing locks will continue to function as
249
- long as a majority of the servers still works.
253
+ If used with a cluster of Redis servers, acquiring and releasing locks will
254
+ continue to function as long as a majority of the servers still works.
250
255
251
256
Example:
252
257
``` php
@@ -259,16 +264,16 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
259
264
$balance -= $amount;
260
265
if ($balance < 0) {
261
266
throw new \DomainException("You have no credit.");
262
-
263
267
}
264
268
$bankAccount->setBalance($balance);
265
269
});
266
270
```
267
271
268
272
#### PredisMutex
269
273
270
- The ** PredisMutex** is the distributed lock implementation of [ RedLock] ( http://redis.io/topics/distlock )
271
- which uses the [ ` Predis ` API] ( https://github.com/nrk/predis ) .
274
+ The ** PredisMutex** is the distributed lock implementation of
275
+ [ RedLock] ( http://redis.io/topics/distlock ) which uses the
276
+ [ ` Predis ` API] ( https://github.com/nrk/predis ) .
272
277
273
278
Example:
274
279
``` php
@@ -280,27 +285,25 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
280
285
$balance -= $amount;
281
286
if ($balance < 0) {
282
287
throw new \DomainException("You have no credit.");
283
-
284
288
}
285
289
$bankAccount->setBalance($balance);
286
290
});
287
291
```
288
292
289
293
#### SemaphoreMutex
290
294
291
- The ** SemaphoreMutex**
292
- is a lock implementation based on [ Semaphore] ( http://php.net/manual/en/ref.sem.php ) .
295
+ The ** SemaphoreMutex** is a lock implementation based on
296
+ [ Semaphore] ( http://php.net/manual/en/ref.sem.php ) .
293
297
294
298
Example:
295
299
``` php
296
300
$semaphore = sem_get(ftok(__FILE__, "a"));
297
- $mutex = new SemaphoreMutex($semaphore);
301
+ $mutex = new SemaphoreMutex($semaphore);
298
302
$mutex->synchronized(function () use ($bankAccount, $amount) {
299
303
$balance = $bankAccount->getBalance();
300
304
$balance -= $amount;
301
305
if ($balance < 0) {
302
306
throw new \DomainException("You have no credit.");
303
-
304
307
}
305
308
$bankAccount->setBalance($balance);
306
309
});
@@ -321,14 +324,15 @@ Example:
321
324
``` php
322
325
$mutex = new TransactionalMutex($pdo);
323
326
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
324
- $select = $pdo->prepare("SELECT balance FROM account WHERE id = ? FOR UPDATE");
327
+ $select = $pdo->prepare(
328
+ "SELECT balance FROM account WHERE id = ? FOR UPDATE"
329
+ );
325
330
$select->execute([$accountId]);
326
331
$balance = $select->fetchColumn();
327
332
328
333
$balance -= $amount;
329
334
if ($balance < 0) {
330
335
throw new \DomainException("You have no credit.");
331
-
332
336
}
333
337
$pdo->prepare("UPDATE account SET balance = ? WHERE id = ?")
334
338
->execute([$balance, $accountId]);
@@ -337,15 +341,16 @@ $mutex->synchronized(function () use ($pdo, $accountId, $amount) {
337
341
338
342
#### MySQLMutex
339
343
340
- The ** MySQLMutex** uses MySQL's
344
+ The ** MySQLMutex** uses MySQL's
341
345
[ ` GET_LOCK ` ] ( https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock )
342
346
function.
343
347
344
- It supports time outs. If the connection to the database server is lost or interrupted, the lock is
345
- automatically released.
348
+ It supports time outs. If the connection to the database server is lost or
349
+ interrupted, the lock is automatically released.
346
350
347
- Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will silently release already
348
- held locks. You should probably refrain from using this mutex on MySQL versions < 5.7.5.
351
+ Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will
352
+ silently release already held locks. You should probably refrain from using this
353
+ mutex on MySQL versions < 5.7.5.
349
354
350
355
``` php
351
356
$pdo = new PDO("mysql:host=localhost;dbname=test", "username");
@@ -356,22 +361,22 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
356
361
$balance -= $amount;
357
362
if ($balance < 0) {
358
363
throw new \DomainException("You have no credit.");
359
-
360
364
}
361
365
$bankAccount->setBalance($balance);
362
366
});
363
367
```
364
368
365
369
#### PgAdvisoryLockMutex
366
370
367
- The ** PgAdvisoryLockMutex** uses PostgreSQL's
371
+ The ** PgAdvisoryLockMutex** uses PostgreSQL's
368
372
[ advisory locking] ( https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS )
369
373
functions.
370
374
371
- Named locks are offered. PostgreSQL locking functions require integers but the conversion is handled automatically.
375
+ Named locks are offered. PostgreSQL locking functions require integers but the
376
+ conversion is handled automatically.
372
377
373
- No time outs are supported. If the connection to the database server is lost or interrupted, the lock is
374
- automatically released.
378
+ No time outs are supported. If the connection to the database server is lost or
379
+ interrupted, the lock is automatically released.
375
380
376
381
``` php
377
382
$pdo = new PDO("pgsql:host=localhost;dbname=test;", "username");
@@ -382,7 +387,6 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
382
387
$balance -= $amount;
383
388
if ($balance < 0) {
384
389
throw new \DomainException("You have no credit.");
385
-
386
390
}
387
391
$bankAccount->setBalance($balance);
388
392
});
@@ -410,4 +414,4 @@ If you like this project and feel generous donate a few Bitcoins here:
410
414
[ 10 ] : https://en.wikipedia.org/wiki/Compare-and-swap
411
415
[ 11 ] : https://github.com/php-lock/lock/blob/master/classes/mutex/CASMutex.php#L44
412
416
[ 12 ] : https://github.com/php-lock/lock/blob/master/classes/mutex/LockMutex.php
413
- [ 13 ] : https://github.com/php-lock/lock/blob/master/classes/exception/LockReleaseException.php
417
+ [ 13 ] : https://github.com/php-lock/lock/blob/master/classes/exception/LockReleaseException.php
0 commit comments