11** [ Requirements] ( #requirements ) ** |
22** [ Installation] ( #installation ) ** |
33** [ Usage] ( #usage ) ** |
4- ** [ License and authors ] ( #license-and-authors ) ** |
4+ ** [ License] ( #license ) ** |
55
66# php-lock/lock
77
@@ -119,7 +119,7 @@ $newBalance = $mutex->check(function () use ($bankAccount, $amount): bool {
119119 return $balance;
120120});
121121
122- if (false === $newBalance ) {
122+ if ($newBalance === false ) {
123123 if ($balance < 0) {
124124 throw new \DomainException('You have no credit.');
125125 }
@@ -143,11 +143,11 @@ try {
143143 throw new \DomainException();
144144 }
145145
146- return " result" ;
146+ return ' result' ;
147147 });
148148} catch (LockReleaseException $unlockException) {
149149 if ($unlockException->getCodeException() !== null) {
150- $codeException = $unlockException->getCodeException()
150+ $codeException = $unlockException->getCodeException();
151151 // do something with the code exception
152152 } else {
153153 $code_result = $unlockException->getCodeResult();
@@ -190,9 +190,9 @@ Example:
190190``` php
191191$mutex = new CASMutex();
192192$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
193- $balance = $memcached->get(" balance" , null, $casToken);
193+ $balance = $memcached->get(' balance' , null, $casToken);
194194 $balance -= $amount;
195- if (!$memcached->cas($casToken, " balance" , $balance)) {
195+ if (!$memcached->cas($casToken, ' balance' , $balance)) {
196196 return;
197197 }
198198 $mutex->notify();
@@ -206,12 +206,12 @@ The **FlockMutex** is a lock implementation based on
206206
207207Example:
208208``` php
209- $mutex = new FlockMutex(fopen(__FILE__, "r" ));
209+ $mutex = new FlockMutex(fopen(__FILE__, 'r' ));
210210$mutex->synchronized(function () use ($bankAccount, $amount) {
211211 $balance = $bankAccount->getBalance();
212212 $balance -= $amount;
213213 if ($balance < 0) {
214- throw new \DomainException(" You have no credit." );
214+ throw new \DomainException(' You have no credit.' );
215215 }
216216 $bankAccount->setBalance($balance);
217217});
@@ -228,14 +228,14 @@ The **MemcachedMutex** is a spinlock implementation which uses the
228228Example:
229229``` php
230230$memcache = new \Memcached();
231- $memcache->addServer(" localhost" , 11211);
231+ $memcache->addServer(' localhost' , 11211);
232232
233- $mutex = new MemcachedMutex(" balance" , $memcache);
233+ $mutex = new MemcachedMutex(' balance' , $memcache);
234234$mutex->synchronized(function () use ($bankAccount, $amount) {
235235 $balance = $bankAccount->getBalance();
236236 $balance -= $amount;
237237 if ($balance < 0) {
238- throw new \DomainException(" You have no credit." );
238+ throw new \DomainException(' You have no credit.' );
239239 }
240240 $bankAccount->setBalance($balance);
241241});
@@ -255,14 +255,14 @@ continue to function as long as a majority of the servers still works.
255255Example:
256256``` php
257257$redis = new Redis();
258- $redis->connect(" localhost" );
258+ $redis->connect(' localhost' );
259259
260- $mutex = new PHPRedisMutex([$redis], " balance" );
260+ $mutex = new PHPRedisMutex([$redis], ' balance' );
261261$mutex->synchronized(function () use ($bankAccount, $amount) {
262262 $balance = $bankAccount->getBalance();
263263 $balance -= $amount;
264264 if ($balance < 0) {
265- throw new \DomainException(" You have no credit." );
265+ throw new \DomainException(' You have no credit.' );
266266 }
267267 $bankAccount->setBalance($balance);
268268});
@@ -276,14 +276,14 @@ The **PredisMutex** is the distributed lock implementation of
276276
277277Example:
278278``` php
279- $redis = new Client(" redis://localhost" );
279+ $redis = new Client(' redis://localhost' );
280280
281- $mutex = new PredisMutex([$redis], " balance" );
281+ $mutex = new PredisMutex([$redis], ' balance' );
282282$mutex->synchronized(function () use ($bankAccount, $amount) {
283283 $balance = $bankAccount->getBalance();
284284 $balance -= $amount;
285285 if ($balance < 0) {
286- throw new \DomainException(" You have no credit." );
286+ throw new \DomainException(' You have no credit.' );
287287 }
288288 $bankAccount->setBalance($balance);
289289});
@@ -296,13 +296,13 @@ The **SemaphoreMutex** is a lock implementation based on
296296
297297Example:
298298``` php
299- $semaphore = sem_get(ftok(__FILE__, "a" ));
299+ $semaphore = sem_get(ftok(__FILE__, 'a' ));
300300$mutex = new SemaphoreMutex($semaphore);
301301$mutex->synchronized(function () use ($bankAccount, $amount) {
302302 $balance = $bankAccount->getBalance();
303303 $balance -= $amount;
304304 if ($balance < 0) {
305- throw new \DomainException(" You have no credit." );
305+ throw new \DomainException(' You have no credit.' );
306306 }
307307 $bankAccount->setBalance($balance);
308308});
@@ -324,16 +324,16 @@ Example:
324324$mutex = new TransactionalMutex($pdo);
325325$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
326326 $select = $pdo->prepare(
327- " SELECT balance FROM account WHERE id = ? FOR UPDATE"
327+ ' SELECT balance FROM account WHERE id = ? FOR UPDATE'
328328 );
329329 $select->execute([$accountId]);
330330 $balance = $select->fetchColumn();
331331
332332 $balance -= $amount;
333333 if ($balance < 0) {
334- throw new \DomainException(" You have no credit." );
334+ throw new \DomainException(' You have no credit.' );
335335 }
336- $pdo->prepare(" UPDATE account SET balance = ? WHERE id = ?" )
336+ $pdo->prepare(' UPDATE account SET balance = ? WHERE id = ?' )
337337 ->execute([$balance, $accountId]);
338338});
339339```
@@ -355,14 +355,14 @@ Also note that `GET_LOCK` function is server wide and the MySQL manual suggests
355355you to namespace your locks like ` dbname.lockname ` .
356356
357357``` php
358- $pdo = new PDO(" mysql:host=localhost;dbname=test", " username" );
358+ $pdo = new PDO(' mysql:host=localhost;dbname=test', ' username' );
359359
360- $mutex = new MySQLMutex($pdo, " balance" , 15);
360+ $mutex = new MySQLMutex($pdo, ' balance' , 15);
361361$mutex->synchronized(function () use ($bankAccount, $amount) {
362362 $balance = $bankAccount->getBalance();
363363 $balance -= $amount;
364364 if ($balance < 0) {
365- throw new \DomainException(" You have no credit." );
365+ throw new \DomainException(' You have no credit.' );
366366 }
367367 $bankAccount->setBalance($balance);
368368});
@@ -381,24 +381,22 @@ No time outs are supported. If the connection to the database server is lost or
381381interrupted, the lock is automatically released.
382382
383383``` php
384- $pdo = new PDO(" pgsql:host=localhost;dbname=test;", " username" );
384+ $pdo = new PDO(' pgsql:host=localhost;dbname=test;', ' username' );
385385
386- $mutex = new PgAdvisoryLockMutex($pdo, " balance" );
386+ $mutex = new PgAdvisoryLockMutex($pdo, ' balance' );
387387$mutex->synchronized(function () use ($bankAccount, $amount) {
388388 $balance = $bankAccount->getBalance();
389389 $balance -= $amount;
390390 if ($balance < 0) {
391- throw new \DomainException(" You have no credit." );
391+ throw new \DomainException(' You have no credit.' );
392392 }
393393 $bankAccount->setBalance($balance);
394394});
395395```
396396
397- ## License and authors
398-
399- This project is free and under the MIT.
397+ ## License
400398
401- Responsible for this project is Willem Stuursma-Ruwen < willem@stuursma.name > .
399+ This project is free and is licensed under the MIT .
402400
403401[ 1 ] : http://semver.org
404402[ 2 ] : https://github.com/nrk/predis
0 commit comments