1 | <?php |
||
41 | class RedisLockStrategy implements LockingStrategyInterface |
||
42 | { |
||
43 | /** |
||
44 | * @var \Redis A key-value data store |
||
45 | */ |
||
46 | private $redis; |
||
47 | |||
48 | /** |
||
49 | * @var string The locking subject, i.e. a string to discriminate the lock |
||
50 | */ |
||
51 | private $subject; |
||
52 | |||
53 | /** |
||
54 | * @var boolean TRUE if lock is acquired |
||
55 | */ |
||
56 | private $isAcquired = false; |
||
57 | |||
58 | /** |
||
59 | 9 | * @var int Seconds the lock remains persistent |
|
60 | */ |
||
61 | 9 | private $ttl = 3600; |
|
62 | |||
63 | 9 | /** |
|
64 | 8 | * @var int Seconds to wait for a lock |
|
65 | */ |
||
66 | private $blTo = 60; |
||
67 | 9 | ||
68 | 2 | /** |
|
69 | * @inheritdoc |
||
70 | */ |
||
71 | 7 | public function __construct($subject) |
|
72 | 1 | { |
|
73 | $config = null; |
||
74 | 6 | ||
75 | if (\array_key_exists('redis_lock', $GLOBALS['TYPO3_CONF_VARS']['SYS'])) { |
||
76 | 6 | $config = $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock']; |
|
77 | 5 | } |
|
78 | |||
79 | if (!\is_array($config)) { |
||
80 | 6 | throw new LockCreateException('no configuration for redis lock strategy found'); |
|
81 | 1 | } |
|
82 | |||
83 | if (!\array_key_exists('host', $config)) { |
||
84 | 5 | throw new LockCreateException('no host for redis lock strategy found'); |
|
85 | 5 | } |
|
86 | $port = 6379; |
||
87 | |||
88 | if (\array_key_exists('port', $config)) { |
||
89 | 5 | $port = (int) $config['port']; |
|
90 | 5 | } |
|
91 | 5 | ||
92 | if (!\array_key_exists('database', $config)) { |
||
93 | 5 | throw new LockCreateException('no database for redis lock strategy found'); |
|
94 | } |
||
95 | |||
96 | if (\array_key_exists('ttl', $config)) { |
||
97 | 5 | $this->ttl = (int) $config['ttl']; |
|
98 | 5 | } |
|
99 | |||
100 | $this->subject = $subject; |
||
101 | $this->redis = new \Redis(); |
||
102 | $this->redis->connect($config['host'], $port); |
||
103 | 9 | ||
104 | if (\array_key_exists('auth', $config)) { |
||
105 | 9 | $this->redis->auth($config['auth']); |
|
106 | } |
||
107 | |||
108 | $this->redis->select((int) $config['database']); |
||
109 | |||
110 | if (!$this->redis->exists($this->subject)) { |
||
111 | 9 | $this->create(); |
|
112 | } |
||
113 | 9 | } |
|
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public static function getCapabilities() |
||
119 | 2 | { |
|
120 | return self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_NOBLOCK; |
||
121 | 2 | } |
|
122 | 1 | ||
123 | /** |
||
124 | * @inheritdoc |
||
125 | 1 | */ |
|
126 | public static function getPriority() |
||
127 | { |
||
128 | return 100; |
||
129 | } |
||
130 | |||
131 | 5 | /** |
|
132 | * @inheritdoc |
||
133 | 5 | */ |
|
134 | public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) |
||
135 | { |
||
136 | if ($this->isAcquired) { |
||
137 | return true; |
||
138 | } |
||
139 | 2 | ||
140 | if ($mode & self::LOCK_CAPABILITY_EXCLUSIVE) { |
||
141 | 2 | ||
142 | 2 | if ($mode & self::LOCK_CAPABILITY_NOBLOCK) { |
|
143 | |||
144 | // this does not block |
||
145 | $this->isAcquired = (bool) $this->redis->lPop($this->subject); |
||
146 | |||
147 | 2 | if (!$this->isAcquired) { |
|
148 | throw new LockAcquireWouldBlockException('could not acquire lock'); |
||
149 | 2 | } |
|
150 | 1 | } else { |
|
151 | |||
152 | // this blocks iff the list is empty |
||
153 | 1 | $this->isAcquired = (bool) $this->redis->blPop([$this->subject], $this->blTo); |
|
154 | |||
155 | if (!$this->isAcquired) { |
||
156 | throw new LockAcquireException('could not acquire lock'); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | } else { |
||
161 | throw new LockAcquireException('insufficient capabilities'); |
||
162 | } |
||
163 | |||
164 | return true; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | public function isAcquired() |
||
171 | { |
||
172 | return $this->isAcquired; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | public function destroy() |
||
179 | { |
||
180 | $this->redis->del($this->subject); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | public function release() |
||
196 | |||
197 | /** |
||
198 | * create synchronization object, i.e. |
||
199 | * a simple list with some single random value |
||
200 | * |
||
201 | * @return boolean TRUE on success |
||
202 | * @throws LockCreateException |
||
203 | */ |
||
204 | private function create() |
||
216 | |||
217 | } |
||
218 |