@@ -71,10 +71,11 @@ The `Mutex` is an abstract class. You will have to chose an implementation:
7171- [ ` PredisMutex ` ] ( #predismutex )
7272- [ ` SemaphoreMutex ` ] ( #semaphoremutex )
7373- [ ` TransactionalMutex ` ] ( #transactionalmutex )
74+ - [ ` MySQLMutex ` ] ( #mysqlmutex )
7475
7576#### CASMutex
7677
77- The [ ` CASMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.CASMutex.html )
78+ The ** CASMutex**
7879has to be used with a [ Compare-and-swap] ( https://en.wikipedia.org/wiki/Compare-and-swap ) operation.
7980This mutex is lock free. It will repeat executing the code until the CAS operation was
8081successful. The code should therefore notify the mutex by calling
@@ -99,8 +100,7 @@ $mutex->synchronized(function () use ($memcached, $mutex, $amount) {
99100
100101#### FlockMutex
101102
102- The [ ` FlockMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.FlockMutex.html )
103- is a lock implementation based on [ ` flock() ` ] ( http://php.net/manual/en/function.flock.php ) .
103+ The ** FlockMutex** is a lock implementation based on [ ` flock() ` ] ( http://php.net/manual/en/function.flock.php ) .
104104
105105Example:
106106``` php
@@ -118,7 +118,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
118118
119119#### MemcachedMutex
120120
121- The [ ` MemcachedMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.MemcachedMutex.html )
121+ The ** MemcachedMutex**
122122is a spinlock implementation which uses the [ ` Memcached ` API] ( http://php.net/manual/en/book.memcached.php ) .
123123
124124Example:
@@ -140,11 +140,10 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
140140
141141#### PHPRedisMutex
142142
143- The [ ` PHPRedisMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.PHPRedisMutex.html )
144- is the distributed lock implementation of [ RedLock] ( http://redis.io/topics/distlock )
143+ The ** PHPRedisMutex** is the distributed lock implementation of [ RedLock] ( http://redis.io/topics/distlock )
145144which uses the [ ` phpredis ` extension] ( https://github.com/phpredis/phpredis ) .
146145
147- This implementation requires at least phpredis-2.2.4.
146+ This implementation requires at least ` phpredis-2.2.4 ` .
148147
149148Example:
150149``` php
@@ -165,8 +164,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
165164
166165#### PredisMutex
167166
168- The [ ` PredisMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.PredisMutex.html )
169- is the distributed lock implementation of [ RedLock] ( http://redis.io/topics/distlock )
167+ The ** PredisMutex** is the distributed lock implementation of [ RedLock] ( http://redis.io/topics/distlock )
170168which uses the [ ` Predis ` API] ( https://github.com/nrk/predis ) .
171169
172170Example:
@@ -187,7 +185,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
187185
188186#### SemaphoreMutex
189187
190- The [ ` SemaphoreMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.SemaphoreMutex.html )
188+ The ** SemaphoreMutex**
191189is a lock implementation based on [ Semaphore] ( http://php.net/manual/en/ref.sem.php ) .
192190
193191Example:
@@ -207,7 +205,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
207205
208206#### TransactionalMutex
209207
210- The [ ` TransactionalMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.TransactionalMutex.html )
208+ The ** TransactionalMutex**
211209delegates the serialization to the DBS. The exclusive code is executed within
212210a transaction. It's up to you to set the correct transaction isolation level.
213211However if the transaction fails (i.e. a ` PDOException ` was thrown), the code
@@ -234,6 +232,36 @@ $mutex->synchronized(function () use ($pdo, $accountId, $amount) {
234232});
235233```
236234
235+
236+ #### MySQLMutex
237+
238+ The ** MySQLMutex** uses MySQL's
239+ [ ` GET_LOCK ` ] ( https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock )
240+ function to create lock back end.
241+
242+ It supports time outs. If the connection to the database server is lost or interrupted, the lock is
243+ automatically released.
244+
245+ Note that before MySQL 5.7.5 you cannot use nested locks, any new lock will silently release already
246+ held locks. You should probably refrain from using this mutex on MySQL versions < 5.7.5.
247+
248+ ``` php
249+ $pdo = new PDO("mysql:host=localhost;dbname=test", "username");
250+
251+ $mutex = new MySQLMutex($pdo, "balance", 15);
252+ $mutex->synchronized(function () use ($bankAccount, $amount) {
253+ $balance = $bankAccount->getBalance();
254+ $balance -= $amount;
255+ if ($balance < 0) {
256+ throw new \DomainException("You have no credit.");
257+
258+ }
259+ $bankAccount->setBalance($balance);
260+ });
261+ ```
262+
263+
264+
237265# License and authors
238266
239267This project is free and under the WTFPL.
0 commit comments