1
1
** [ Requirements] ( #requirements ) ** |
2
2
** [ Installation] ( #installation ) ** |
3
3
** [ Usage] ( #usage ) ** |
4
- ** [ License and authors ] ( #license-and-authors ) ** |
4
+ ** [ License] ( #license ) ** |
5
5
6
6
# php-lock/lock
7
7
@@ -119,7 +119,7 @@ $newBalance = $mutex->check(function () use ($bankAccount, $amount): bool {
119
119
return $balance;
120
120
});
121
121
122
- if (false === $newBalance ) {
122
+ if ($newBalance === false ) {
123
123
if ($balance < 0) {
124
124
throw new \DomainException('You have no credit.');
125
125
}
@@ -143,11 +143,11 @@ try {
143
143
throw new \DomainException();
144
144
}
145
145
146
- return " result" ;
146
+ return ' result' ;
147
147
});
148
148
} catch (LockReleaseException $unlockException) {
149
149
if ($unlockException->getCodeException() !== null) {
150
- $codeException = $unlockException->getCodeException()
150
+ $codeException = $unlockException->getCodeException();
151
151
// do something with the code exception
152
152
} else {
153
153
$code_result = $unlockException->getCodeResult();
@@ -190,9 +190,9 @@ Example:
190
190
``` php
191
191
$mutex = new CASMutex();
192
192
$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
193
- $balance = $memcached->get(" balance" , null, $casToken);
193
+ $balance = $memcached->get(' balance' , null, $casToken);
194
194
$balance -= $amount;
195
- if (!$memcached->cas($casToken, " balance" , $balance)) {
195
+ if (!$memcached->cas($casToken, ' balance' , $balance)) {
196
196
return;
197
197
}
198
198
$mutex->notify();
@@ -206,12 +206,12 @@ The **FlockMutex** is a lock implementation based on
206
206
207
207
Example:
208
208
``` php
209
- $mutex = new FlockMutex(fopen(__FILE__, "r" ));
209
+ $mutex = new FlockMutex(fopen(__FILE__, 'r' ));
210
210
$mutex->synchronized(function () use ($bankAccount, $amount) {
211
211
$balance = $bankAccount->getBalance();
212
212
$balance -= $amount;
213
213
if ($balance < 0) {
214
- throw new \DomainException(" You have no credit." );
214
+ throw new \DomainException(' You have no credit.' );
215
215
}
216
216
$bankAccount->setBalance($balance);
217
217
});
@@ -228,14 +228,14 @@ The **MemcachedMutex** is a spinlock implementation which uses the
228
228
Example:
229
229
``` php
230
230
$memcache = new \Memcached();
231
- $memcache->addServer(" localhost" , 11211);
231
+ $memcache->addServer(' localhost' , 11211);
232
232
233
- $mutex = new MemcachedMutex(" balance" , $memcache);
233
+ $mutex = new MemcachedMutex(' balance' , $memcache);
234
234
$mutex->synchronized(function () use ($bankAccount, $amount) {
235
235
$balance = $bankAccount->getBalance();
236
236
$balance -= $amount;
237
237
if ($balance < 0) {
238
- throw new \DomainException(" You have no credit." );
238
+ throw new \DomainException(' You have no credit.' );
239
239
}
240
240
$bankAccount->setBalance($balance);
241
241
});
@@ -255,14 +255,14 @@ continue to function as long as a majority of the servers still works.
255
255
Example:
256
256
``` php
257
257
$redis = new Redis();
258
- $redis->connect(" localhost" );
258
+ $redis->connect(' localhost' );
259
259
260
- $mutex = new PHPRedisMutex([$redis], " balance" );
260
+ $mutex = new PHPRedisMutex([$redis], ' balance' );
261
261
$mutex->synchronized(function () use ($bankAccount, $amount) {
262
262
$balance = $bankAccount->getBalance();
263
263
$balance -= $amount;
264
264
if ($balance < 0) {
265
- throw new \DomainException(" You have no credit." );
265
+ throw new \DomainException(' You have no credit.' );
266
266
}
267
267
$bankAccount->setBalance($balance);
268
268
});
@@ -276,14 +276,14 @@ The **PredisMutex** is the distributed lock implementation of
276
276
277
277
Example:
278
278
``` php
279
- $redis = new Client(" redis://localhost" );
279
+ $redis = new Client(' redis://localhost' );
280
280
281
- $mutex = new PredisMutex([$redis], " balance" );
281
+ $mutex = new PredisMutex([$redis], ' balance' );
282
282
$mutex->synchronized(function () use ($bankAccount, $amount) {
283
283
$balance = $bankAccount->getBalance();
284
284
$balance -= $amount;
285
285
if ($balance < 0) {
286
- throw new \DomainException(" You have no credit." );
286
+ throw new \DomainException(' You have no credit.' );
287
287
}
288
288
$bankAccount->setBalance($balance);
289
289
});
@@ -296,13 +296,13 @@ The **SemaphoreMutex** is a lock implementation based on
296
296
297
297
Example:
298
298
``` php
299
- $semaphore = sem_get(ftok(__FILE__, "a" ));
299
+ $semaphore = sem_get(ftok(__FILE__, 'a' ));
300
300
$mutex = new SemaphoreMutex($semaphore);
301
301
$mutex->synchronized(function () use ($bankAccount, $amount) {
302
302
$balance = $bankAccount->getBalance();
303
303
$balance -= $amount;
304
304
if ($balance < 0) {
305
- throw new \DomainException(" You have no credit." );
305
+ throw new \DomainException(' You have no credit.' );
306
306
}
307
307
$bankAccount->setBalance($balance);
308
308
});
@@ -324,16 +324,16 @@ Example:
324
324
$mutex = new TransactionalMutex($pdo);
325
325
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
326
326
$select = $pdo->prepare(
327
- " SELECT balance FROM account WHERE id = ? FOR UPDATE"
327
+ ' SELECT balance FROM account WHERE id = ? FOR UPDATE'
328
328
);
329
329
$select->execute([$accountId]);
330
330
$balance = $select->fetchColumn();
331
331
332
332
$balance -= $amount;
333
333
if ($balance < 0) {
334
- throw new \DomainException(" You have no credit." );
334
+ throw new \DomainException(' You have no credit.' );
335
335
}
336
- $pdo->prepare(" UPDATE account SET balance = ? WHERE id = ?" )
336
+ $pdo->prepare(' UPDATE account SET balance = ? WHERE id = ?' )
337
337
->execute([$balance, $accountId]);
338
338
});
339
339
```
@@ -355,14 +355,14 @@ Also note that `GET_LOCK` function is server wide and the MySQL manual suggests
355
355
you to namespace your locks like ` dbname.lockname ` .
356
356
357
357
``` php
358
- $pdo = new PDO(" mysql:host=localhost;dbname=test", " username" );
358
+ $pdo = new PDO(' mysql:host=localhost;dbname=test', ' username' );
359
359
360
- $mutex = new MySQLMutex($pdo, " balance" , 15);
360
+ $mutex = new MySQLMutex($pdo, ' balance' , 15);
361
361
$mutex->synchronized(function () use ($bankAccount, $amount) {
362
362
$balance = $bankAccount->getBalance();
363
363
$balance -= $amount;
364
364
if ($balance < 0) {
365
- throw new \DomainException(" You have no credit." );
365
+ throw new \DomainException(' You have no credit.' );
366
366
}
367
367
$bankAccount->setBalance($balance);
368
368
});
@@ -381,24 +381,22 @@ No time outs are supported. If the connection to the database server is lost or
381
381
interrupted, the lock is automatically released.
382
382
383
383
``` php
384
- $pdo = new PDO(" pgsql:host=localhost;dbname=test;", " username" );
384
+ $pdo = new PDO(' pgsql:host=localhost;dbname=test;', ' username' );
385
385
386
- $mutex = new PgAdvisoryLockMutex($pdo, " balance" );
386
+ $mutex = new PgAdvisoryLockMutex($pdo, ' balance' );
387
387
$mutex->synchronized(function () use ($bankAccount, $amount) {
388
388
$balance = $bankAccount->getBalance();
389
389
$balance -= $amount;
390
390
if ($balance < 0) {
391
- throw new \DomainException(" You have no credit." );
391
+ throw new \DomainException(' You have no credit.' );
392
392
}
393
393
$bankAccount->setBalance($balance);
394
394
});
395
395
```
396
396
397
- ## License and authors
398
-
399
- This project is free and under the MIT.
397
+ ## License
400
398
401
- Responsible for this project is
Willem Stuursma-Ruwen < [email protected] > .
399
+ This project is free and is licensed under the MIT .
402
400
403
401
[ 1 ] : http://semver.org
404
402
[ 2 ] : https://github.com/nrk/predis
0 commit comments