|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of ninja-mutex. |
|
4
|
|
|
* |
|
5
|
|
|
* (C) Kamil Dziedzic <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace NinjaMutex\Lock; |
|
11
|
|
|
|
|
12
|
|
|
use Memcached; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Lock implementor using Memcached |
|
16
|
|
|
* |
|
17
|
|
|
* @author Kamil Dziedzic <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class MemcachedLock extends LockAbstract implements LockExpirationInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Maximum expiration time in seconds (30 days) |
|
23
|
|
|
* http://php.net/manual/en/memcached.add.php |
|
24
|
|
|
*/ |
|
25
|
|
|
const MAX_EXPIRATION = 2592000; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Memcache connection |
|
29
|
|
|
* |
|
30
|
|
|
* @var Memcached |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $memcached; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int Expiration time of the lock in seconds |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $expiration = 0; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Memcached $memcached |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function __construct($memcached) |
|
43
|
|
|
{ |
|
44
|
1 |
|
parent::__construct(); |
|
45
|
|
|
|
|
46
|
1 |
|
$this->memcached = $memcached; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param int $expiration Expiration time of the lock in seconds. If it's equal to zero (default), the lock will never expire. |
|
51
|
|
|
* Max 2592000s (30 days), if greater it will be capped to 2592000 without throwing an error. |
|
52
|
|
|
* WARNING: Using value higher than 0 may lead to race conditions. If you set too low expiration time |
|
53
|
|
|
* e.g. 30s and critical section will run for 31s another process will gain lock at the same time, |
|
54
|
|
|
* leading to unpredicted behaviour. Use with caution. |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setExpiration($expiration) |
|
57
|
|
|
{ |
|
58
|
1 |
|
if ($expiration > static::MAX_EXPIRATION) { |
|
59
|
|
|
$expiration = static::MAX_EXPIRATION; |
|
60
|
|
|
} |
|
61
|
1 |
|
$this->expiration = $expiration; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Clear lock without releasing it |
|
66
|
|
|
* Do not use this method unless you know what you do |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name name of lock |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function clearLock($name) |
|
72
|
|
|
{ |
|
73
|
1 |
|
if (!isset($this->locks[$name])) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
unset($this->locks[$name]); |
|
78
|
1 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $name name of lock |
|
83
|
|
|
* @param bool $blocking |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
34 |
|
protected function getLock($name, $blocking) |
|
87
|
|
|
{ |
|
88
|
34 |
|
if (!$this->memcached->add($name, serialize($this->getLockInformation()), $this->expiration)) { |
|
89
|
5 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
34 |
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Release lock |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name name of lock |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
34 |
|
public function releaseLock($name) |
|
102
|
|
|
{ |
|
103
|
34 |
|
if (isset($this->locks[$name]) && $this->memcached->delete($name)) { |
|
104
|
34 |
|
unset($this->locks[$name]); |
|
105
|
|
|
|
|
106
|
34 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
5 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Check if lock is locked |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $name name of lock |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
15 |
|
public function isLocked($name) |
|
119
|
|
|
{ |
|
120
|
15 |
|
return false !== $this->memcached->get($name); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|