@@ -71,10 +71,11 @@ The `Mutex` is an abstract class. You will have to chose an implementation:
71
71
- [ ` PredisMutex ` ] ( #predismutex )
72
72
- [ ` SemaphoreMutex ` ] ( #semaphoremutex )
73
73
- [ ` TransactionalMutex ` ] ( #transactionalmutex )
74
+ - [ ` MySQLMutex ` ] ( #mysqlmutex )
74
75
75
76
#### CASMutex
76
77
77
- The [ ` CASMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.CASMutex.html )
78
+ The ** CASMutex**
78
79
has to be used with a [ Compare-and-swap] ( https://en.wikipedia.org/wiki/Compare-and-swap ) operation.
79
80
This mutex is lock free. It will repeat executing the code until the CAS operation was
80
81
successful. The code should therefore notify the mutex by calling
@@ -99,8 +100,7 @@ $mutex->synchronized(function () use ($memcached, $mutex, $amount) {
99
100
100
101
#### FlockMutex
101
102
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 ) .
104
104
105
105
Example:
106
106
``` php
@@ -118,7 +118,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
118
118
119
119
#### MemcachedMutex
120
120
121
- The [ ` MemcachedMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.MemcachedMutex.html )
121
+ The ** MemcachedMutex**
122
122
is a spinlock implementation which uses the [ ` Memcached ` API] ( http://php.net/manual/en/book.memcached.php ) .
123
123
124
124
Example:
@@ -140,11 +140,10 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
140
140
141
141
#### PHPRedisMutex
142
142
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 )
145
144
which uses the [ ` phpredis ` extension] ( https://github.com/phpredis/phpredis ) .
146
145
147
- This implementation requires at least phpredis-2.2.4.
146
+ This implementation requires at least ` phpredis-2.2.4 ` .
148
147
149
148
Example:
150
149
``` php
@@ -165,8 +164,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
165
164
166
165
#### PredisMutex
167
166
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 )
170
168
which uses the [ ` Predis ` API] ( https://github.com/nrk/predis ) .
171
169
172
170
Example:
@@ -187,7 +185,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
187
185
188
186
#### SemaphoreMutex
189
187
190
- The [ ` SemaphoreMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.SemaphoreMutex.html )
188
+ The ** SemaphoreMutex**
191
189
is a lock implementation based on [ Semaphore] ( http://php.net/manual/en/ref.sem.php ) .
192
190
193
191
Example:
@@ -207,7 +205,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
207
205
208
206
#### TransactionalMutex
209
207
210
- The [ ` TransactionalMutex ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.TransactionalMutex.html )
208
+ The ** TransactionalMutex**
211
209
delegates the serialization to the DBS. The exclusive code is executed within
212
210
a transaction. It's up to you to set the correct transaction isolation level.
213
211
However if the transaction fails (i.e. a ` PDOException ` was thrown), the code
@@ -234,6 +232,36 @@ $mutex->synchronized(function () use ($pdo, $accountId, $amount) {
234
232
});
235
233
```
236
234
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
+
237
265
# License and authors
238
266
239
267
This project is free and under the WTFPL.
0 commit comments