1
|
|
|
<?php namespace Comodojo\Cache\Providers; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Cache\Traits\StatefulTrait; |
4
|
|
|
use \Comodojo\Cache\Traits\NamespaceTrait; |
5
|
|
|
use \Comodojo\Cache\Interfaces\EnhancedCacheItemPoolInterface; |
6
|
|
|
use \Comodojo\Cache\Item; |
7
|
|
|
use \Comodojo\Cache\Components\KeyValidator; |
8
|
|
|
use \Comodojo\Foundation\Utils\UniqueId; |
9
|
|
|
use \Comodojo\Foundation\Utils\ClassProperties; |
10
|
|
|
use \Psr\Log\LoggerInterface; |
11
|
|
|
use \Psr\Cache\CacheItemInterface; |
12
|
|
|
use \DateTime; |
13
|
|
|
use \Comodojo\Exception\CacheException; |
14
|
|
|
use \Comodojo\Exception\InvalidCacheArgumentException; |
15
|
|
|
use \Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract stateful provider implementation |
19
|
|
|
* |
20
|
|
|
* @package Comodojo Spare Parts |
21
|
|
|
* @author Marco Giovinazzi <[email protected]> |
22
|
|
|
* @license MIT |
23
|
|
|
* |
24
|
|
|
* LICENSE: |
25
|
|
|
* |
26
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
27
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
28
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
29
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
30
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
31
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
32
|
|
|
* THE SOFTWARE. |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
abstract class AbstractEnhancedProvider |
36
|
|
|
extends AbstractProvider |
|
|
|
|
37
|
|
|
implements EnhancedCacheItemPoolInterface { |
|
|
|
|
38
|
|
|
|
39
|
|
|
use StatefulTrait; |
40
|
|
|
use NamespaceTrait; |
41
|
|
|
|
42
|
|
|
protected $driver; |
43
|
|
|
|
44
|
|
|
protected $default_properties = []; |
45
|
|
|
|
46
|
|
|
protected $properties; |
47
|
|
|
|
48
|
|
|
private $queue = []; |
49
|
|
|
|
50
|
208 |
View Code Duplication |
public function __construct(array $properties = [], LoggerInterface $logger = null) { |
|
|
|
|
51
|
|
|
|
52
|
208 |
|
parent::__construct($logger); |
53
|
|
|
|
54
|
208 |
|
$this->properties = ClassProperties::create($this->default_properties)->merge($properties); |
55
|
|
|
|
56
|
208 |
|
$this->setId(UniqueId::generate(64)); |
57
|
|
|
|
58
|
208 |
|
} |
59
|
|
|
|
60
|
|
|
abstract public function getStats(); |
61
|
|
|
|
62
|
106 |
|
public function getProperties() { |
63
|
|
|
|
64
|
106 |
|
return $this->properties; |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
179 |
|
public function getItem($key) { |
69
|
|
|
|
70
|
174 |
|
if ( KeyValidator::validateKey($key) === false ) { |
71
|
|
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
|
76
|
179 |
|
$data = $this->driver->get($key, $this->getNamespace()); |
77
|
|
|
|
78
|
174 |
|
} catch (Exception $e) { |
79
|
|
|
|
80
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
81
|
|
|
$data = null; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
174 |
|
if ( $data === null ) return new Item($key); |
86
|
|
|
|
87
|
93 |
|
$item = new Item($key, true); |
88
|
|
|
|
89
|
93 |
|
return $item->set(unserialize($data)); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
41 |
View Code Duplication |
public function hasItem($key) { |
|
|
|
|
94
|
|
|
|
95
|
41 |
|
if ( KeyValidator::validateKey($key) === false ) { |
96
|
|
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
|
101
|
41 |
|
$data = $this->driver->has($key, $this->getNamespace()); |
102
|
|
|
|
103
|
41 |
|
} catch (Exception $e) { |
104
|
|
|
|
105
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
106
|
|
|
$data = false; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
41 |
|
return $data; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
56 |
|
public function clear() { |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
|
118
|
56 |
|
$data = $this->driver->clear(); |
119
|
|
|
|
120
|
56 |
|
} catch (Exception $e) { |
121
|
|
|
|
122
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
123
|
|
|
$data = false; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
56 |
|
return $data; |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
7 |
View Code Duplication |
public function clearNamespace() { |
|
|
|
|
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
|
135
|
7 |
|
$data = $this->driver->clear($this->getNamespace()); |
136
|
|
|
|
137
|
7 |
|
} catch (Exception $e) { |
138
|
|
|
|
139
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
140
|
|
|
$data = false; |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
7 |
|
return $data; |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
10 |
View Code Duplication |
public function deleteItem($key) { |
|
|
|
|
149
|
|
|
|
150
|
10 |
|
if ( KeyValidator::validateKey($key) === false ) { |
151
|
|
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
|
156
|
10 |
|
$data = $this->driver->delete($key, $this->getNamespace()); |
157
|
|
|
|
158
|
10 |
|
} catch (Exception $e) { |
159
|
|
|
|
160
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
161
|
|
|
$data = false; |
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
10 |
|
return $data; |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
168 |
|
public function save(CacheItemInterface $item) { |
170
|
|
|
|
171
|
168 |
|
$ttl = $item->getTtl(); |
172
|
|
|
|
173
|
168 |
|
if ( $ttl < 0 ) return false; |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
|
177
|
119 |
|
$data = $this->driver->set($item->getKey(), $this->getNamespace(), serialize($item->getRaw()), $ttl); |
178
|
|
|
|
179
|
119 |
|
} catch (Exception $e) { |
180
|
|
|
|
181
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
182
|
|
|
$data = false; |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
119 |
|
return $data; |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
208 |
View Code Duplication |
public function test() { |
|
|
|
|
191
|
|
|
|
192
|
208 |
|
if ( $this->driver->test() ) { |
193
|
|
|
|
194
|
208 |
|
$this->setState(self::CACHE_SUCCESS); |
195
|
|
|
|
196
|
208 |
|
return true; |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$error = $this->driver->getName()." driver unavailable, disabling provider ".$this->getId()." administratively"; |
201
|
|
|
|
202
|
|
|
$this->logger->error($error); |
203
|
|
|
|
204
|
|
|
$this->setState(self::CACHE_ERROR, $error); |
205
|
|
|
|
206
|
|
|
return false; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
12 |
|
public function getItems(array $keys = []) { |
211
|
|
|
|
212
|
12 |
|
if ( empty($keys) ) return []; |
213
|
|
|
|
214
|
6 |
|
$result = []; |
215
|
|
|
|
216
|
|
|
try { |
217
|
|
|
|
218
|
6 |
|
$data = $this->driver->getMultiple($keys, $this->getNamespace()); |
219
|
|
|
|
220
|
6 |
|
} catch (Exception $e) { |
221
|
|
|
|
222
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
223
|
|
|
$data = array_combine($keys, array_fill(0, count($keys), null)); |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
6 |
|
foreach ( $data as $key => $value ) { |
228
|
|
|
|
229
|
6 |
|
if ( $value == null ) { |
230
|
|
|
$result[$key] = new Item($key); |
231
|
|
|
} else { |
232
|
6 |
|
$result[$key] = new Item($key, true); |
233
|
6 |
|
$result[$key]->set(unserialize($value)); |
234
|
|
|
} |
235
|
|
|
|
236
|
6 |
|
} |
237
|
|
|
|
238
|
6 |
|
return $result; |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
12 |
View Code Duplication |
public function deleteItems(array $keys) { |
|
|
|
|
243
|
|
|
|
244
|
|
|
try { |
245
|
|
|
|
246
|
12 |
|
$data = $this->driver->deleteMultiple($keys, $this->getNamespace()); |
247
|
|
|
|
248
|
12 |
|
} catch (Exception $e) { |
249
|
|
|
|
250
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
251
|
|
|
$data = false; |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
12 |
|
return $data; |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
12 |
View Code Duplication |
public function saveDeferred(CacheItemInterface $item) { |
|
|
|
|
260
|
|
|
|
261
|
12 |
|
$this->checkQueueNamespace(true); |
262
|
|
|
|
263
|
12 |
|
$namespace = $this->getNamespace(); |
264
|
|
|
|
265
|
12 |
|
$this->queue[$namespace][$item->getKey()] = $item; |
266
|
|
|
|
267
|
12 |
|
return true; |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
12 |
View Code Duplication |
public function commit() { |
|
|
|
|
272
|
|
|
|
273
|
12 |
|
$result = []; |
274
|
|
|
|
275
|
12 |
|
$active_namespace = $this->getNamespace(); |
276
|
|
|
|
277
|
12 |
|
foreach ( $this->queue as $namespace => $queue ) { |
278
|
|
|
|
279
|
12 |
|
$this->setNamespace($namespace); |
280
|
|
|
|
281
|
12 |
|
foreach ( $queue as $key => $item ) { |
282
|
|
|
|
283
|
12 |
|
$result[] = $this->save($item); |
284
|
|
|
|
285
|
12 |
|
} |
286
|
|
|
|
287
|
12 |
|
} |
288
|
|
|
|
289
|
12 |
|
$this->queue = []; |
290
|
|
|
|
291
|
12 |
|
$this->setNamespace($active_namespace); |
292
|
|
|
|
293
|
12 |
|
return in_array(false, $result) ? false : true; |
294
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
12 |
View Code Duplication |
private function checkQueueNamespace($create = false) { |
|
|
|
|
298
|
|
|
|
299
|
12 |
|
$namespace = $this->getNamespace(); |
300
|
|
|
|
301
|
12 |
|
if ( array_key_exists($namespace, $this->queue) ) { |
302
|
6 |
|
return true; |
303
|
12 |
|
} else if ( $create ) { |
304
|
12 |
|
$this->queue[$namespace] = []; |
305
|
12 |
|
return true; |
306
|
|
|
} else { |
307
|
|
|
return false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
} |
313
|
|
|
|