|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\mutex; |
|
9
|
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* MysqlMutex implements mutex "lock" mechanism via MySQL locks. |
|
14
|
|
|
* |
|
15
|
|
|
* Application configuration example: |
|
16
|
|
|
* |
|
17
|
|
|
* ``` |
|
18
|
|
|
* [ |
|
19
|
|
|
* 'components' => [ |
|
20
|
|
|
* 'db' => [ |
|
21
|
|
|
* 'class' => 'yii\db\Connection', |
|
22
|
|
|
* 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', |
|
23
|
|
|
* ] |
|
24
|
|
|
* 'mutex' => [ |
|
25
|
|
|
* 'class' => 'yii\mutex\MysqlMutex', |
|
26
|
|
|
* ], |
|
27
|
|
|
* ], |
|
28
|
|
|
* ] |
|
29
|
|
|
* ``` |
|
30
|
|
|
* |
|
31
|
|
|
* @see Mutex |
|
32
|
|
|
* |
|
33
|
|
|
* @author resurtm <[email protected]> |
|
34
|
|
|
* @since 2.0 |
|
35
|
|
|
*/ |
|
36
|
|
|
class MysqlMutex extends DbMutex |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Initializes MySQL specific mutex component implementation. |
|
40
|
|
|
* @throws InvalidConfigException if [[db]] is not MySQL connection. |
|
41
|
|
|
*/ |
|
42
|
10 |
|
public function init() |
|
43
|
|
|
{ |
|
44
|
10 |
|
parent::init(); |
|
45
|
10 |
|
if ($this->db->driverName !== 'mysql') { |
|
46
|
|
|
throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.'); |
|
47
|
|
|
} |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Acquires lock by given name. |
|
52
|
|
|
* @param string $name of the lock to be acquired. |
|
53
|
|
|
* @param int $timeout time (in seconds) to wait for lock to become released. |
|
54
|
|
|
* @return bool acquiring result. |
|
55
|
|
|
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock |
|
56
|
|
|
*/ |
|
57
|
10 |
|
protected function acquireLock($name, $timeout = 0) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->db->useMaster(function ($db) use ($name, $timeout) { |
|
60
|
|
|
/** @var \yii\db\Connection $db */ |
|
61
|
10 |
|
return (bool) $db->createCommand( |
|
62
|
10 |
|
'SELECT GET_LOCK(:name, :timeout)', |
|
63
|
10 |
|
[':name' => $this->hashLockName($name), ':timeout' => $timeout] |
|
64
|
10 |
|
)->queryScalar(); |
|
65
|
10 |
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Releases lock by given name. |
|
70
|
|
|
* @param string $name of the lock to be released. |
|
71
|
|
|
* @return bool release result. |
|
72
|
|
|
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function releaseLock($name) |
|
75
|
|
|
{ |
|
76
|
10 |
|
return $this->db->useMaster(function ($db) use ($name) { |
|
77
|
|
|
/** @var \yii\db\Connection $db */ |
|
78
|
10 |
|
return (bool) $db->createCommand( |
|
79
|
10 |
|
'SELECT RELEASE_LOCK(:name)', |
|
80
|
10 |
|
[':name' => $this->hashLockName($name)] |
|
81
|
10 |
|
)->queryScalar(); |
|
82
|
10 |
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Generate hash for lock name to avoid exceeding lock name length limit. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @return string |
|
90
|
|
|
* @since 2.0.16 |
|
91
|
|
|
* @see https://github.com/yiisoft/yii2/pull/16836 |
|
92
|
|
|
*/ |
|
93
|
10 |
|
protected function hashLockName($name) { |
|
94
|
10 |
|
return sha1($name); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|