Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 3, 2024
1 parent e1b12c3 commit 68eef18
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ $newBalance = $mutex->check(function () use ($bankAccount, $amount): bool {
return $balance;
});

if (false === $newBalance) {
if ($newBalance === false) {
if ($balance < 0) {
throw new \DomainException('You have no credit.');
}
Expand All @@ -143,11 +143,11 @@ try {
throw new \DomainException();
}

return "result";
return 'result';
});
} catch (LockReleaseException $unlockException) {
if ($unlockException->getCodeException() !== null) {
$codeException = $unlockException->getCodeException()
$codeException = $unlockException->getCodeException();
// do something with the code exception
} else {
$code_result = $unlockException->getCodeResult();
Expand Down Expand Up @@ -190,9 +190,9 @@ Example:
```php
$mutex = new CASMutex();
$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
$balance = $memcached->get("balance", null, $casToken);
$balance = $memcached->get('balance', null, $casToken);
$balance -= $amount;
if (!$memcached->cas($casToken, "balance", $balance)) {
if (!$memcached->cas($casToken, 'balance', $balance)) {
return;
}
$mutex->notify();
Expand All @@ -206,12 +206,12 @@ The **FlockMutex** is a lock implementation based on

Example:
```php
$mutex = new FlockMutex(fopen(__FILE__, "r"));
$mutex = new FlockMutex(fopen(__FILE__, 'r'));
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -228,14 +228,14 @@ The **MemcachedMutex** is a spinlock implementation which uses the
Example:
```php
$memcache = new \Memcached();
$memcache->addServer("localhost", 11211);
$memcache->addServer('localhost', 11211);

$mutex = new MemcachedMutex("balance", $memcache);
$mutex = new MemcachedMutex('balance', $memcache);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -255,14 +255,14 @@ continue to function as long as a majority of the servers still works.
Example:
```php
$redis = new Redis();
$redis->connect("localhost");
$redis->connect('localhost');

$mutex = new PHPRedisMutex([$redis], "balance");
$mutex = new PHPRedisMutex([$redis], 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -276,14 +276,14 @@ The **PredisMutex** is the distributed lock implementation of

Example:
```php
$redis = new Client("redis://localhost");
$redis = new Client('redis://localhost');

$mutex = new PredisMutex([$redis], "balance");
$mutex = new PredisMutex([$redis], 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -296,13 +296,13 @@ The **SemaphoreMutex** is a lock implementation based on

Example:
```php
$semaphore = sem_get(ftok(__FILE__, "a"));
$semaphore = sem_get(ftok(__FILE__, 'a'));
$mutex = new SemaphoreMutex($semaphore);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -324,16 +324,16 @@ Example:
$mutex = new TransactionalMutex($pdo);
$mutex->synchronized(function () use ($pdo, $accountId, $amount) {
$select = $pdo->prepare(
"SELECT balance FROM account WHERE id = ? FOR UPDATE"
'SELECT balance FROM account WHERE id = ? FOR UPDATE'
);
$select->execute([$accountId]);
$balance = $select->fetchColumn();

$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$pdo->prepare("UPDATE account SET balance = ? WHERE id = ?")
$pdo->prepare('UPDATE account SET balance = ? WHERE id = ?')
->execute([$balance, $accountId]);
});
```
Expand All @@ -355,14 +355,14 @@ Also note that `GET_LOCK` function is server wide and the MySQL manual suggests
you to namespace your locks like `dbname.lockname`.

```php
$pdo = new PDO("mysql:host=localhost;dbname=test", "username");
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username');

$mutex = new MySQLMutex($pdo, "balance", 15);
$mutex = new MySQLMutex($pdo, 'balance', 15);
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand All @@ -381,14 +381,14 @@ No time outs are supported. If the connection to the database server is lost or
interrupted, the lock is automatically released.

```php
$pdo = new PDO("pgsql:host=localhost;dbname=test;", "username");
$pdo = new PDO('pgsql:host=localhost;dbname=test;', 'username');

$mutex = new PgAdvisoryLockMutex($pdo, "balance");
$mutex = new PgAdvisoryLockMutex($pdo, 'balance');
$mutex->synchronized(function () use ($bankAccount, $amount) {
$balance = $bankAccount->getBalance();
$balance -= $amount;
if ($balance < 0) {
throw new \DomainException("You have no credit.");
throw new \DomainException('You have no credit.');
}
$bankAccount->setBalance($balance);
});
Expand Down
1 change: 0 additions & 1 deletion src/mutex/CASMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function notify(): void
* $balance -= $amount;
* if (!$memcached->cas($casToken, 'balance', $balance)) {
* return;
*
* }
* $mutex->notify();
* });
Expand Down
4 changes: 2 additions & 2 deletions src/mutex/SemaphoreMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class SemaphoreMutex extends LockMutex
*
* Example:
* <code>
* $semaphore = sem_get(ftok(__FILE__, "a"));
* $mutex = new SemaphoreMutex($semaphore);
* $semaphore = sem_get(ftok(__FILE__, 'a'));
* $mutex = new SemaphoreMutex($semaphore);
* </code>
*
* @param \SysvSemaphore|resource $semaphore the semaphore id
Expand Down
4 changes: 2 additions & 2 deletions tests/mutex/PredisMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected function setUp(): void
{
parent::setUp();

$this->client = $this->getMockBuilder(ClientInterface::class)
->setMethods(array_merge(get_class_methods(ClientInterface::class), ['set', 'eval'])) // @phpstan-ignore method.deprecated
$this->client = $this->getMockBuilder(ClientInterface::class) // @phpstan-ignore method.deprecated
->setMethods(array_merge(get_class_methods(ClientInterface::class), ['set', 'eval']))
->getMock();

$this->mutex = new PredisMutex([$this->client], 'test', 2.5);
Expand Down

0 comments on commit 68eef18

Please sign in to comment.