1
|
|
|
<?php |
2
|
|
|
namespace pastuhov\yii2redismutex; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\mutex\Mutex; |
6
|
|
|
use yii\redis\Connection; |
7
|
|
|
use yii\di\Instance; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Redis mutex. |
11
|
|
|
* |
12
|
|
|
* @link http://redis.io/topics/distlock |
13
|
|
|
* @see Mutex |
14
|
|
|
*/ |
15
|
|
|
class RedisMutex extends Mutex |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \yii\redis\Connection |
20
|
|
|
*/ |
21
|
|
|
public $redis; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Expire time, in seconds. |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
public $expireTime = 3; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
* |
33
|
|
|
* @throws \yii\base\InvalidConfigException |
34
|
|
|
*/ |
35
|
3 |
|
public function init() |
36
|
|
|
{ |
37
|
3 |
|
parent::init(); |
38
|
3 |
|
$this->redis = Instance::ensure($this->redis, Connection::className()); |
39
|
|
|
|
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Acquires lock by given name. |
44
|
|
|
* |
45
|
|
|
* @param string $name of the lock to be acquired. |
46
|
|
|
* @param integer $timeout to wait for lock to become released. |
47
|
|
|
* |
48
|
|
|
* @return boolean acquiring result. |
49
|
|
|
*/ |
50
|
3 |
|
protected function acquireLock ($name, $timeout = 0) |
51
|
|
|
{ |
52
|
3 |
|
$redis = $this->getConnection(); |
53
|
3 |
|
$lockValue = $this->getLockValue(); |
54
|
|
|
$params = [ |
55
|
3 |
|
$name, // Key name |
56
|
3 |
|
$lockValue, // Key value |
57
|
3 |
|
'NX', // Set if Not eXists |
58
|
3 |
|
'EX', // Expire time |
59
|
3 |
|
$this->expireTime // Seconds |
60
|
2 |
|
]; |
61
|
3 |
|
$response = $redis->executeCommand('SET', $params); |
62
|
|
|
|
63
|
3 |
|
if ($response === true) { |
64
|
|
|
|
65
|
3 |
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Releases lock by given name. |
73
|
|
|
* |
74
|
|
|
* @param string $name of the lock to be released. |
75
|
|
|
* |
76
|
|
|
* @return boolean release result. |
77
|
|
|
*/ |
78
|
3 |
|
protected function releaseLock ($name) |
79
|
|
|
{ |
80
|
3 |
|
$redis = $this->getConnection(); |
81
|
3 |
|
$lockValue = $this->getLockValue(); |
82
|
|
|
$script = ' |
83
|
|
|
if redis.call("get",KEYS[1]) == ARGV[1] |
84
|
|
|
then |
85
|
|
|
return redis.call("del",KEYS[1]) |
86
|
|
|
else |
87
|
|
|
return 0 |
88
|
|
|
end |
89
|
3 |
|
'; |
90
|
|
|
$params = [ |
91
|
3 |
|
$script, // Lua script |
92
|
3 |
|
1, // the number of arguments that follows the script (starting from the third argument) |
93
|
3 |
|
$name, // Key name |
94
|
1 |
|
$lockValue // Key value |
95
|
2 |
|
]; |
96
|
|
|
|
97
|
3 |
|
$redis->executeCommand('EVAL', $params); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return \yii\redis\Connection |
102
|
|
|
*/ |
103
|
3 |
|
protected function getConnection() |
104
|
|
|
{ |
105
|
3 |
|
return $this->redis; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var float|false |
110
|
|
|
*/ |
111
|
|
|
private $_lockValue = false; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return float|mixed |
115
|
|
|
*/ |
116
|
3 |
|
public function getLockValue() |
117
|
|
|
{ |
118
|
3 |
|
if ($this->_lockValue === false) { |
119
|
3 |
|
$this->_lockValue = microtime(true); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
3 |
|
return $this->_lockValue; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|