1 | <?php |
||
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) |
|
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) |
|
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) |
|
80 | |||
81 | /** |
||
82 | * @param string $name name of lock |
||
83 | * @param bool $blocking |
||
84 | * @return bool |
||
85 | */ |
||
86 | 34 | protected function getLock($name, $blocking) |
|
94 | |||
95 | /** |
||
96 | * Release lock |
||
97 | * |
||
98 | * @param string $name name of lock |
||
99 | * @return bool |
||
100 | */ |
||
101 | 34 | public function releaseLock($name) |
|
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) |
|
122 | } |
||
123 | |||
124 |