@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | public function setItem(CacheItemInterface $item) |
67 | 67 | { |
68 | 68 | if ($this->getClassNamespace() . '\\Item' === \get_class($item)) { |
69 | - if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
69 | + if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
70 | 70 | throw new PhpfastcacheLogicException( |
71 | 71 | 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
72 | 72 | ); |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | * Reset the Item |
209 | 209 | */ |
210 | 210 | $item->set(null) |
211 | - ->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])) |
|
211 | + ->expiresAfter(abs((int) $this->getConfig()['defaultTtl'])) |
|
212 | 212 | ->setHit(false) |
213 | 213 | ->setTags([]); |
214 | 214 | if ($this->getConfig()->isItemDetailedDate()) { |
@@ -224,15 +224,15 @@ discard block |
||
224 | 224 | $item->setHit(true); |
225 | 225 | } |
226 | 226 | } else { |
227 | - $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
|
227 | + $item->expiresAfter(abs((int) $this->getConfig()['defaultTtl'])); |
|
228 | 228 | } |
229 | 229 | } |
230 | - }else{ |
|
230 | + } else { |
|
231 | 231 | $item = $this->itemInstances[$key]; |
232 | 232 | } |
233 | 233 | |
234 | 234 | |
235 | - if($item !== null){ |
|
235 | + if ($item !== null) { |
|
236 | 236 | /** |
237 | 237 | * @eventName CacheGetItem |
238 | 238 | * @param $this ExtendedCacheItemPoolInterface |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | } |
294 | 294 | } |
295 | 295 | |
296 | - return (bool)$return; |
|
296 | + return (bool) $return; |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | } |
382 | 382 | } |
383 | 383 | |
384 | - return (bool)$return; |
|
384 | + return (bool) $return; |
|
385 | 385 | } |
386 | 386 | |
387 | 387 | /** |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | * loop dispatching operations |
402 | 402 | */ |
403 | 403 | if (!isset($this->itemInstances[$item->getKey()])) { |
404 | - if($this->getConfig()->isUseStaticItemCaching()){ |
|
404 | + if ($this->getConfig()->isUseStaticItemCaching()) { |
|
405 | 405 | $this->itemInstances[$item->getKey()] = $item; |
406 | 406 | } |
407 | 407 | } else { |
@@ -39,413 +39,413 @@ |
||
39 | 39 | */ |
40 | 40 | trait CacheItemPoolTrait |
41 | 41 | { |
42 | - use ClassNamespaceResolverTrait; |
|
43 | - use EventManagerDispatcherTrait; |
|
44 | - use TaggableCacheItemPoolTrait; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - protected static $unsupportedKeyChars = '{}()/\@:'; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var array |
|
53 | - */ |
|
54 | - protected $deferredList = []; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var ExtendedCacheItemInterface[] |
|
58 | - */ |
|
59 | - protected $itemInstances = []; |
|
60 | - |
|
61 | - /**CacheItemPoolTrait |
|
42 | + use ClassNamespaceResolverTrait; |
|
43 | + use EventManagerDispatcherTrait; |
|
44 | + use TaggableCacheItemPoolTrait; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + protected static $unsupportedKeyChars = '{}()/\@:'; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var array |
|
53 | + */ |
|
54 | + protected $deferredList = []; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var ExtendedCacheItemInterface[] |
|
58 | + */ |
|
59 | + protected $itemInstances = []; |
|
60 | + |
|
61 | + /**CacheItemPoolTrait |
|
62 | 62 | * @param CacheItemInterface $item |
63 | 63 | * @return $this |
64 | 64 | * @throws PhpfastcacheInvalidArgumentException |
65 | 65 | */ |
66 | - public function setItem(CacheItemInterface $item) |
|
67 | - { |
|
68 | - if ($this->getClassNamespace() . '\\Item' === \get_class($item)) { |
|
69 | - if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
70 | - throw new PhpfastcacheLogicException( |
|
71 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
72 | - ); |
|
73 | - } |
|
74 | - |
|
75 | - $this->itemInstances[$item->getKey()] = $item; |
|
76 | - |
|
77 | - return $this; |
|
78 | - } |
|
79 | - throw new PhpfastcacheInvalidArgumentException( |
|
80 | - \sprintf( |
|
81 | - 'Invalid Item Class "%s" for this driver "%s".', |
|
82 | - get_class($item), |
|
83 | - get_class($this) |
|
84 | - ) |
|
85 | - ); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param array $keys |
|
90 | - * @return array |
|
91 | - * @throws PhpfastcacheCoreException |
|
92 | - * @throws PhpfastcacheInvalidArgumentException |
|
93 | - * @throws PhpfastcacheLogicException |
|
94 | - */ |
|
95 | - public function getItems(array $keys = []) |
|
96 | - { |
|
97 | - $collection = []; |
|
98 | - foreach ($keys as $key) { |
|
99 | - $collection[$key] = $this->getItem($key); |
|
100 | - } |
|
101 | - |
|
102 | - return $collection; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $key |
|
107 | - * @return ExtendedCacheItemInterface |
|
108 | - * @throws PhpfastcacheInvalidArgumentException |
|
109 | - * @throws PhpfastcacheLogicException |
|
110 | - * @throws PhpfastcacheCoreException |
|
111 | - */ |
|
112 | - public function getItem($key) |
|
113 | - { |
|
114 | - if (\is_string($key)) { |
|
115 | - $item = null; |
|
116 | - |
|
117 | - /** |
|
118 | - * Replace array_key_exists by isset |
|
119 | - * due to performance issue on huge |
|
120 | - * loop dispatching operations |
|
121 | - */ |
|
122 | - if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
123 | - if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
124 | - throw new PhpfastcacheInvalidArgumentException( |
|
125 | - 'Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
|
126 | - ); |
|
127 | - } |
|
128 | - |
|
129 | - $cacheSlamsSpendSeconds = 0; |
|
130 | - $class = $this->getClassNamespace() . '\Item'; |
|
131 | - /** @var $item ExtendedCacheItemInterface */ |
|
132 | - $item = new $class($this, $key); |
|
133 | - $item->setEventManager($this->eventManager); |
|
134 | - |
|
135 | - getItemDriverRead: |
|
136 | - { |
|
137 | - $driverArray = $this->driverRead($item); |
|
138 | - |
|
139 | - if ($driverArray) { |
|
140 | - if (!\is_array($driverArray)) { |
|
141 | - throw new PhpfastcacheCoreException( |
|
142 | - sprintf( |
|
143 | - 'The driverRead method returned an unexpected variable type: %s', |
|
144 | - \gettype($driverArray) |
|
145 | - ) |
|
146 | - ); |
|
147 | - } |
|
148 | - $driverData = $this->driverUnwrapData($driverArray); |
|
149 | - |
|
150 | - if ($this->getConfig()['preventCacheSlams']) { |
|
151 | - while ($driverData instanceof ItemBatch) { |
|
152 | - if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
|
153 | - /** |
|
154 | - * The timeout has been reached |
|
155 | - * Consider that the batch has |
|
156 | - * failed and serve an empty item |
|
157 | - * to avoid to get stuck with a |
|
158 | - * batch item stored in driver |
|
159 | - */ |
|
160 | - goto getItemDriverExpired; |
|
161 | - } |
|
162 | - /** |
|
163 | - * @eventName CacheGetItem |
|
164 | - * @param $this ExtendedCacheItemPoolInterface |
|
165 | - * @param $driverData ItemBatch |
|
166 | - * @param $cacheSlamsSpendSeconds int |
|
167 | - */ |
|
168 | - $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); |
|
169 | - |
|
170 | - /** |
|
171 | - * Wait for a second before |
|
172 | - * attempting to get exit |
|
173 | - * the current batch process |
|
174 | - */ |
|
175 | - \sleep(1); |
|
176 | - $cacheSlamsSpendSeconds++; |
|
177 | - goto getItemDriverRead; |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - $item->set($driverData); |
|
182 | - $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
183 | - |
|
184 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
185 | - /** |
|
186 | - * If the itemDetailedDate has been |
|
187 | - * set after caching, we MUST inject |
|
188 | - * a new DateTime object on the fly |
|
189 | - */ |
|
190 | - $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
191 | - $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
192 | - } |
|
193 | - |
|
194 | - $item->setTags($this->driverUnwrapTags($driverArray)); |
|
195 | - |
|
196 | - getItemDriverExpired: |
|
197 | - if ($item->isExpired()) { |
|
198 | - /** |
|
199 | - * Using driverDelete() instead of delete() |
|
200 | - * to avoid infinite loop caused by |
|
201 | - * getItem() call in delete() method |
|
202 | - * As we MUST return an item in any |
|
203 | - * way, we do not de-register here |
|
204 | - */ |
|
205 | - $this->driverDelete($item); |
|
206 | - |
|
207 | - /** |
|
208 | - * Reset the Item |
|
209 | - */ |
|
210 | - $item->set(null) |
|
211 | - ->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])) |
|
212 | - ->setHit(false) |
|
213 | - ->setTags([]); |
|
214 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
215 | - /** |
|
216 | - * If the itemDetailedDate has been |
|
217 | - * set after caching, we MUST inject |
|
218 | - * a new DateTime object on the fly |
|
219 | - */ |
|
220 | - $item->setCreationDate(new DateTime()); |
|
221 | - $item->setModificationDate(new DateTime()); |
|
222 | - } |
|
223 | - } else { |
|
224 | - $item->setHit(true); |
|
225 | - } |
|
226 | - } else { |
|
227 | - $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
|
228 | - } |
|
229 | - } |
|
230 | - }else{ |
|
231 | - $item = $this->itemInstances[$key]; |
|
232 | - } |
|
233 | - |
|
234 | - |
|
235 | - if($item !== null){ |
|
236 | - /** |
|
237 | - * @eventName CacheGetItem |
|
238 | - * @param $this ExtendedCacheItemPoolInterface |
|
239 | - * @param $this ExtendedCacheItemInterface |
|
240 | - */ |
|
241 | - $this->eventManager->dispatch('CacheGetItem', $this, $item); |
|
242 | - |
|
243 | - $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
244 | - |
|
245 | - return $item; |
|
246 | - } |
|
247 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Item %s was not build due to an unknown error', \gettype($key))); |
|
248 | - } |
|
249 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key))); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @param string $key |
|
254 | - * @return bool |
|
255 | - * @throws PhpfastcacheInvalidArgumentException |
|
256 | - */ |
|
257 | - public function hasItem($key) |
|
258 | - { |
|
259 | - return $this->getItem($key)->isHit(); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @return bool |
|
264 | - */ |
|
265 | - public function clear() |
|
266 | - { |
|
267 | - /** |
|
268 | - * @eventName CacheClearItem |
|
269 | - * @param $this ExtendedCacheItemPoolInterface |
|
270 | - * @param $itemInstances ExtendedCacheItemInterface[] |
|
271 | - */ |
|
272 | - $this->eventManager->dispatch('CacheClearItem', $this, $this->itemInstances); |
|
273 | - |
|
274 | - $this->getIO()->incWriteHit(); |
|
275 | - // Faster than detachAllItems() |
|
276 | - $this->itemInstances = []; |
|
277 | - |
|
278 | - return $this->driverClear(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param array $keys |
|
283 | - * @return bool |
|
284 | - * @throws PhpfastcacheInvalidArgumentException |
|
285 | - */ |
|
286 | - public function deleteItems(array $keys) |
|
287 | - { |
|
288 | - $return = null; |
|
289 | - foreach ($keys as $key) { |
|
290 | - $result = $this->deleteItem($key); |
|
291 | - if ($result !== false) { |
|
292 | - $return = $result; |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - return (bool)$return; |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @param string $key |
|
301 | - * @return bool |
|
302 | - * @throws PhpfastcacheInvalidArgumentException |
|
303 | - */ |
|
304 | - public function deleteItem($key) |
|
305 | - { |
|
306 | - $item = $this->getItem($key); |
|
307 | - if ($item->isHit() && $this->driverDelete($item)) { |
|
308 | - $item->setHit(false); |
|
309 | - $this->getIO()->incWriteHit(); |
|
310 | - |
|
311 | - /** |
|
312 | - * @eventName CacheCommitItem |
|
313 | - * @param $this ExtendedCacheItemPoolInterface |
|
314 | - * @param $item ExtendedCacheItemInterface |
|
315 | - */ |
|
316 | - $this->eventManager->dispatch('CacheDeleteItem', $this, $item); |
|
317 | - |
|
318 | - /** |
|
319 | - * De-register the item instance |
|
320 | - * then collect gc cycles |
|
321 | - */ |
|
322 | - $this->deregisterItem($key); |
|
323 | - |
|
324 | - /** |
|
325 | - * Perform a tag cleanup to avoid memory leaks |
|
326 | - */ |
|
327 | - if (\strpos($key, self::DRIVER_TAGS_KEY_PREFIX) !== 0) { |
|
328 | - $this->cleanItemTags($item); |
|
329 | - } |
|
330 | - |
|
331 | - return true; |
|
332 | - } |
|
333 | - |
|
334 | - return false; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @param CacheItemInterface $item |
|
339 | - * @return CacheItemInterface |
|
340 | - * @throws RuntimeException |
|
341 | - */ |
|
342 | - public function saveDeferred(CacheItemInterface $item) |
|
343 | - { |
|
344 | - if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
345 | - $this->itemInstances[$item->getKey()] = $item; |
|
346 | - } else { |
|
347 | - if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
348 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
349 | - } |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * @eventName CacheSaveDeferredItem |
|
354 | - * @param $this ExtendedCacheItemPoolInterface |
|
355 | - * @param $this ExtendedCacheItemInterface |
|
356 | - */ |
|
357 | - $this->eventManager->dispatch('CacheSaveDeferredItem', $this, $item); |
|
358 | - |
|
359 | - return $this->deferredList[$item->getKey()] = $item; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @return bool |
|
364 | - * @throws PhpfastcacheInvalidArgumentException |
|
365 | - */ |
|
366 | - public function commit() |
|
367 | - { |
|
368 | - /** |
|
369 | - * @eventName CacheCommitItem |
|
370 | - * @param $this ExtendedCacheItemPoolInterface |
|
371 | - * @param $deferredList ExtendedCacheItemInterface[] |
|
372 | - */ |
|
373 | - $this->eventManager->dispatch('CacheCommitItem', $this, $this->deferredList); |
|
374 | - |
|
375 | - $return = null; |
|
376 | - foreach ($this->deferredList as $key => $item) { |
|
377 | - $result = $this->save($item); |
|
378 | - if ($return !== false) { |
|
379 | - unset($this->deferredList[$key]); |
|
380 | - $return = $result; |
|
381 | - } |
|
382 | - } |
|
383 | - |
|
384 | - return (bool)$return; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * @param CacheItemInterface $item |
|
389 | - * @return bool |
|
390 | - * @throws PhpfastcacheInvalidArgumentException |
|
391 | - * @throws PhpfastcacheLogicException |
|
392 | - * @throws \ReflectionException |
|
393 | - */ |
|
394 | - public function save(CacheItemInterface $item) |
|
395 | - { |
|
396 | - /** |
|
397 | - * @var ExtendedCacheItemInterface $item |
|
398 | - * |
|
399 | - * Replace array_key_exists by isset |
|
400 | - * due to performance issue on huge |
|
401 | - * loop dispatching operations |
|
402 | - */ |
|
403 | - if (!isset($this->itemInstances[$item->getKey()])) { |
|
404 | - if($this->getConfig()->isUseStaticItemCaching()){ |
|
405 | - $this->itemInstances[$item->getKey()] = $item; |
|
406 | - } |
|
407 | - } else { |
|
408 | - if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
409 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
410 | - } |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * @eventName CacheSaveItem |
|
415 | - * @param $this ExtendedCacheItemPoolInterface |
|
416 | - * @param $this ExtendedCacheItemInterface |
|
417 | - */ |
|
418 | - $this->eventManager->dispatch('CacheSaveItem', $this, $item); |
|
419 | - |
|
420 | - |
|
421 | - if ($this->getConfig()->isPreventCacheSlams()) { |
|
422 | - /** |
|
423 | - * @var $itemBatch ExtendedCacheItemInterface |
|
424 | - */ |
|
425 | - $class = new ReflectionClass((new ReflectionObject($this))->getNamespaceName() . '\Item'); |
|
426 | - $itemBatch = $class->newInstanceArgs([$this, $item->getKey()]); |
|
427 | - $itemBatch->setEventManager($this->eventManager) |
|
428 | - ->set(new ItemBatch($item->getKey(), new DateTime())) |
|
429 | - ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
430 | - |
|
431 | - /** |
|
432 | - * To avoid SPL mismatches |
|
433 | - * we have to re-attach the |
|
434 | - * original item to the pool |
|
435 | - */ |
|
436 | - $this->driverWrite($itemBatch); |
|
437 | - $this->detachItem($itemBatch); |
|
438 | - $this->attachItem($item); |
|
439 | - } |
|
440 | - |
|
441 | - |
|
442 | - if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
443 | - $item->setHit(true); |
|
444 | - $this->getIO()->incWriteHit(); |
|
445 | - |
|
446 | - return true; |
|
447 | - } |
|
448 | - |
|
449 | - return false; |
|
450 | - } |
|
66 | + public function setItem(CacheItemInterface $item) |
|
67 | + { |
|
68 | + if ($this->getClassNamespace() . '\\Item' === \get_class($item)) { |
|
69 | + if(!$this->getConfig()->isUseStaticItemCaching()){ |
|
70 | + throw new PhpfastcacheLogicException( |
|
71 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
72 | + ); |
|
73 | + } |
|
74 | + |
|
75 | + $this->itemInstances[$item->getKey()] = $item; |
|
76 | + |
|
77 | + return $this; |
|
78 | + } |
|
79 | + throw new PhpfastcacheInvalidArgumentException( |
|
80 | + \sprintf( |
|
81 | + 'Invalid Item Class "%s" for this driver "%s".', |
|
82 | + get_class($item), |
|
83 | + get_class($this) |
|
84 | + ) |
|
85 | + ); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param array $keys |
|
90 | + * @return array |
|
91 | + * @throws PhpfastcacheCoreException |
|
92 | + * @throws PhpfastcacheInvalidArgumentException |
|
93 | + * @throws PhpfastcacheLogicException |
|
94 | + */ |
|
95 | + public function getItems(array $keys = []) |
|
96 | + { |
|
97 | + $collection = []; |
|
98 | + foreach ($keys as $key) { |
|
99 | + $collection[$key] = $this->getItem($key); |
|
100 | + } |
|
101 | + |
|
102 | + return $collection; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $key |
|
107 | + * @return ExtendedCacheItemInterface |
|
108 | + * @throws PhpfastcacheInvalidArgumentException |
|
109 | + * @throws PhpfastcacheLogicException |
|
110 | + * @throws PhpfastcacheCoreException |
|
111 | + */ |
|
112 | + public function getItem($key) |
|
113 | + { |
|
114 | + if (\is_string($key)) { |
|
115 | + $item = null; |
|
116 | + |
|
117 | + /** |
|
118 | + * Replace array_key_exists by isset |
|
119 | + * due to performance issue on huge |
|
120 | + * loop dispatching operations |
|
121 | + */ |
|
122 | + if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
123 | + if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
124 | + throw new PhpfastcacheInvalidArgumentException( |
|
125 | + 'Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
|
126 | + ); |
|
127 | + } |
|
128 | + |
|
129 | + $cacheSlamsSpendSeconds = 0; |
|
130 | + $class = $this->getClassNamespace() . '\Item'; |
|
131 | + /** @var $item ExtendedCacheItemInterface */ |
|
132 | + $item = new $class($this, $key); |
|
133 | + $item->setEventManager($this->eventManager); |
|
134 | + |
|
135 | + getItemDriverRead: |
|
136 | + { |
|
137 | + $driverArray = $this->driverRead($item); |
|
138 | + |
|
139 | + if ($driverArray) { |
|
140 | + if (!\is_array($driverArray)) { |
|
141 | + throw new PhpfastcacheCoreException( |
|
142 | + sprintf( |
|
143 | + 'The driverRead method returned an unexpected variable type: %s', |
|
144 | + \gettype($driverArray) |
|
145 | + ) |
|
146 | + ); |
|
147 | + } |
|
148 | + $driverData = $this->driverUnwrapData($driverArray); |
|
149 | + |
|
150 | + if ($this->getConfig()['preventCacheSlams']) { |
|
151 | + while ($driverData instanceof ItemBatch) { |
|
152 | + if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
|
153 | + /** |
|
154 | + * The timeout has been reached |
|
155 | + * Consider that the batch has |
|
156 | + * failed and serve an empty item |
|
157 | + * to avoid to get stuck with a |
|
158 | + * batch item stored in driver |
|
159 | + */ |
|
160 | + goto getItemDriverExpired; |
|
161 | + } |
|
162 | + /** |
|
163 | + * @eventName CacheGetItem |
|
164 | + * @param $this ExtendedCacheItemPoolInterface |
|
165 | + * @param $driverData ItemBatch |
|
166 | + * @param $cacheSlamsSpendSeconds int |
|
167 | + */ |
|
168 | + $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); |
|
169 | + |
|
170 | + /** |
|
171 | + * Wait for a second before |
|
172 | + * attempting to get exit |
|
173 | + * the current batch process |
|
174 | + */ |
|
175 | + \sleep(1); |
|
176 | + $cacheSlamsSpendSeconds++; |
|
177 | + goto getItemDriverRead; |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + $item->set($driverData); |
|
182 | + $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
183 | + |
|
184 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
185 | + /** |
|
186 | + * If the itemDetailedDate has been |
|
187 | + * set after caching, we MUST inject |
|
188 | + * a new DateTime object on the fly |
|
189 | + */ |
|
190 | + $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
191 | + $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
192 | + } |
|
193 | + |
|
194 | + $item->setTags($this->driverUnwrapTags($driverArray)); |
|
195 | + |
|
196 | + getItemDriverExpired: |
|
197 | + if ($item->isExpired()) { |
|
198 | + /** |
|
199 | + * Using driverDelete() instead of delete() |
|
200 | + * to avoid infinite loop caused by |
|
201 | + * getItem() call in delete() method |
|
202 | + * As we MUST return an item in any |
|
203 | + * way, we do not de-register here |
|
204 | + */ |
|
205 | + $this->driverDelete($item); |
|
206 | + |
|
207 | + /** |
|
208 | + * Reset the Item |
|
209 | + */ |
|
210 | + $item->set(null) |
|
211 | + ->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])) |
|
212 | + ->setHit(false) |
|
213 | + ->setTags([]); |
|
214 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
215 | + /** |
|
216 | + * If the itemDetailedDate has been |
|
217 | + * set after caching, we MUST inject |
|
218 | + * a new DateTime object on the fly |
|
219 | + */ |
|
220 | + $item->setCreationDate(new DateTime()); |
|
221 | + $item->setModificationDate(new DateTime()); |
|
222 | + } |
|
223 | + } else { |
|
224 | + $item->setHit(true); |
|
225 | + } |
|
226 | + } else { |
|
227 | + $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
|
228 | + } |
|
229 | + } |
|
230 | + }else{ |
|
231 | + $item = $this->itemInstances[$key]; |
|
232 | + } |
|
233 | + |
|
234 | + |
|
235 | + if($item !== null){ |
|
236 | + /** |
|
237 | + * @eventName CacheGetItem |
|
238 | + * @param $this ExtendedCacheItemPoolInterface |
|
239 | + * @param $this ExtendedCacheItemInterface |
|
240 | + */ |
|
241 | + $this->eventManager->dispatch('CacheGetItem', $this, $item); |
|
242 | + |
|
243 | + $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
244 | + |
|
245 | + return $item; |
|
246 | + } |
|
247 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Item %s was not build due to an unknown error', \gettype($key))); |
|
248 | + } |
|
249 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key))); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @param string $key |
|
254 | + * @return bool |
|
255 | + * @throws PhpfastcacheInvalidArgumentException |
|
256 | + */ |
|
257 | + public function hasItem($key) |
|
258 | + { |
|
259 | + return $this->getItem($key)->isHit(); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @return bool |
|
264 | + */ |
|
265 | + public function clear() |
|
266 | + { |
|
267 | + /** |
|
268 | + * @eventName CacheClearItem |
|
269 | + * @param $this ExtendedCacheItemPoolInterface |
|
270 | + * @param $itemInstances ExtendedCacheItemInterface[] |
|
271 | + */ |
|
272 | + $this->eventManager->dispatch('CacheClearItem', $this, $this->itemInstances); |
|
273 | + |
|
274 | + $this->getIO()->incWriteHit(); |
|
275 | + // Faster than detachAllItems() |
|
276 | + $this->itemInstances = []; |
|
277 | + |
|
278 | + return $this->driverClear(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param array $keys |
|
283 | + * @return bool |
|
284 | + * @throws PhpfastcacheInvalidArgumentException |
|
285 | + */ |
|
286 | + public function deleteItems(array $keys) |
|
287 | + { |
|
288 | + $return = null; |
|
289 | + foreach ($keys as $key) { |
|
290 | + $result = $this->deleteItem($key); |
|
291 | + if ($result !== false) { |
|
292 | + $return = $result; |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + return (bool)$return; |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @param string $key |
|
301 | + * @return bool |
|
302 | + * @throws PhpfastcacheInvalidArgumentException |
|
303 | + */ |
|
304 | + public function deleteItem($key) |
|
305 | + { |
|
306 | + $item = $this->getItem($key); |
|
307 | + if ($item->isHit() && $this->driverDelete($item)) { |
|
308 | + $item->setHit(false); |
|
309 | + $this->getIO()->incWriteHit(); |
|
310 | + |
|
311 | + /** |
|
312 | + * @eventName CacheCommitItem |
|
313 | + * @param $this ExtendedCacheItemPoolInterface |
|
314 | + * @param $item ExtendedCacheItemInterface |
|
315 | + */ |
|
316 | + $this->eventManager->dispatch('CacheDeleteItem', $this, $item); |
|
317 | + |
|
318 | + /** |
|
319 | + * De-register the item instance |
|
320 | + * then collect gc cycles |
|
321 | + */ |
|
322 | + $this->deregisterItem($key); |
|
323 | + |
|
324 | + /** |
|
325 | + * Perform a tag cleanup to avoid memory leaks |
|
326 | + */ |
|
327 | + if (\strpos($key, self::DRIVER_TAGS_KEY_PREFIX) !== 0) { |
|
328 | + $this->cleanItemTags($item); |
|
329 | + } |
|
330 | + |
|
331 | + return true; |
|
332 | + } |
|
333 | + |
|
334 | + return false; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @param CacheItemInterface $item |
|
339 | + * @return CacheItemInterface |
|
340 | + * @throws RuntimeException |
|
341 | + */ |
|
342 | + public function saveDeferred(CacheItemInterface $item) |
|
343 | + { |
|
344 | + if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
345 | + $this->itemInstances[$item->getKey()] = $item; |
|
346 | + } else { |
|
347 | + if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
348 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
349 | + } |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * @eventName CacheSaveDeferredItem |
|
354 | + * @param $this ExtendedCacheItemPoolInterface |
|
355 | + * @param $this ExtendedCacheItemInterface |
|
356 | + */ |
|
357 | + $this->eventManager->dispatch('CacheSaveDeferredItem', $this, $item); |
|
358 | + |
|
359 | + return $this->deferredList[$item->getKey()] = $item; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @return bool |
|
364 | + * @throws PhpfastcacheInvalidArgumentException |
|
365 | + */ |
|
366 | + public function commit() |
|
367 | + { |
|
368 | + /** |
|
369 | + * @eventName CacheCommitItem |
|
370 | + * @param $this ExtendedCacheItemPoolInterface |
|
371 | + * @param $deferredList ExtendedCacheItemInterface[] |
|
372 | + */ |
|
373 | + $this->eventManager->dispatch('CacheCommitItem', $this, $this->deferredList); |
|
374 | + |
|
375 | + $return = null; |
|
376 | + foreach ($this->deferredList as $key => $item) { |
|
377 | + $result = $this->save($item); |
|
378 | + if ($return !== false) { |
|
379 | + unset($this->deferredList[$key]); |
|
380 | + $return = $result; |
|
381 | + } |
|
382 | + } |
|
383 | + |
|
384 | + return (bool)$return; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * @param CacheItemInterface $item |
|
389 | + * @return bool |
|
390 | + * @throws PhpfastcacheInvalidArgumentException |
|
391 | + * @throws PhpfastcacheLogicException |
|
392 | + * @throws \ReflectionException |
|
393 | + */ |
|
394 | + public function save(CacheItemInterface $item) |
|
395 | + { |
|
396 | + /** |
|
397 | + * @var ExtendedCacheItemInterface $item |
|
398 | + * |
|
399 | + * Replace array_key_exists by isset |
|
400 | + * due to performance issue on huge |
|
401 | + * loop dispatching operations |
|
402 | + */ |
|
403 | + if (!isset($this->itemInstances[$item->getKey()])) { |
|
404 | + if($this->getConfig()->isUseStaticItemCaching()){ |
|
405 | + $this->itemInstances[$item->getKey()] = $item; |
|
406 | + } |
|
407 | + } else { |
|
408 | + if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
409 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
410 | + } |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * @eventName CacheSaveItem |
|
415 | + * @param $this ExtendedCacheItemPoolInterface |
|
416 | + * @param $this ExtendedCacheItemInterface |
|
417 | + */ |
|
418 | + $this->eventManager->dispatch('CacheSaveItem', $this, $item); |
|
419 | + |
|
420 | + |
|
421 | + if ($this->getConfig()->isPreventCacheSlams()) { |
|
422 | + /** |
|
423 | + * @var $itemBatch ExtendedCacheItemInterface |
|
424 | + */ |
|
425 | + $class = new ReflectionClass((new ReflectionObject($this))->getNamespaceName() . '\Item'); |
|
426 | + $itemBatch = $class->newInstanceArgs([$this, $item->getKey()]); |
|
427 | + $itemBatch->setEventManager($this->eventManager) |
|
428 | + ->set(new ItemBatch($item->getKey(), new DateTime())) |
|
429 | + ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
430 | + |
|
431 | + /** |
|
432 | + * To avoid SPL mismatches |
|
433 | + * we have to re-attach the |
|
434 | + * original item to the pool |
|
435 | + */ |
|
436 | + $this->driverWrite($itemBatch); |
|
437 | + $this->detachItem($itemBatch); |
|
438 | + $this->attachItem($item); |
|
439 | + } |
|
440 | + |
|
441 | + |
|
442 | + if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
443 | + $item->setHit(true); |
|
444 | + $this->getIO()->incWriteHit(); |
|
445 | + |
|
446 | + return true; |
|
447 | + } |
|
448 | + |
|
449 | + return false; |
|
450 | + } |
|
451 | 451 | } |
@@ -63,8 +63,7 @@ discard block |
||
63 | 63 | * @return $this |
64 | 64 | * @throws PhpfastcacheInvalidArgumentException |
65 | 65 | */ |
66 | - public function setItem(CacheItemInterface $item) |
|
67 | - { |
|
66 | + public function setItem(CacheItemInterface $item) { |
|
68 | 67 | if ($this->getClassNamespace() . '\\Item' === \get_class($item)) { |
69 | 68 | if(!$this->getConfig()->isUseStaticItemCaching()){ |
70 | 69 | throw new PhpfastcacheLogicException( |
@@ -92,8 +91,7 @@ discard block |
||
92 | 91 | * @throws PhpfastcacheInvalidArgumentException |
93 | 92 | * @throws PhpfastcacheLogicException |
94 | 93 | */ |
95 | - public function getItems(array $keys = []) |
|
96 | - { |
|
94 | + public function getItems(array $keys = []) { |
|
97 | 95 | $collection = []; |
98 | 96 | foreach ($keys as $key) { |
99 | 97 | $collection[$key] = $this->getItem($key); |
@@ -109,8 +107,7 @@ discard block |
||
109 | 107 | * @throws PhpfastcacheLogicException |
110 | 108 | * @throws PhpfastcacheCoreException |
111 | 109 | */ |
112 | - public function getItem($key) |
|
113 | - { |
|
110 | + public function getItem($key) { |
|
114 | 111 | if (\is_string($key)) { |
115 | 112 | $item = null; |
116 | 113 | |
@@ -220,14 +217,17 @@ discard block |
||
220 | 217 | $item->setCreationDate(new DateTime()); |
221 | 218 | $item->setModificationDate(new DateTime()); |
222 | 219 | } |
223 | - } else { |
|
220 | + } |
|
221 | + else { |
|
224 | 222 | $item->setHit(true); |
225 | 223 | } |
226 | - } else { |
|
224 | + } |
|
225 | + else { |
|
227 | 226 | $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
228 | 227 | } |
229 | 228 | } |
230 | - }else{ |
|
229 | + } |
|
230 | + else{ |
|
231 | 231 | $item = $this->itemInstances[$key]; |
232 | 232 | } |
233 | 233 | |
@@ -254,16 +254,14 @@ discard block |
||
254 | 254 | * @return bool |
255 | 255 | * @throws PhpfastcacheInvalidArgumentException |
256 | 256 | */ |
257 | - public function hasItem($key) |
|
258 | - { |
|
257 | + public function hasItem($key) { |
|
259 | 258 | return $this->getItem($key)->isHit(); |
260 | 259 | } |
261 | 260 | |
262 | 261 | /** |
263 | 262 | * @return bool |
264 | 263 | */ |
265 | - public function clear() |
|
266 | - { |
|
264 | + public function clear() { |
|
267 | 265 | /** |
268 | 266 | * @eventName CacheClearItem |
269 | 267 | * @param $this ExtendedCacheItemPoolInterface |
@@ -283,8 +281,7 @@ discard block |
||
283 | 281 | * @return bool |
284 | 282 | * @throws PhpfastcacheInvalidArgumentException |
285 | 283 | */ |
286 | - public function deleteItems(array $keys) |
|
287 | - { |
|
284 | + public function deleteItems(array $keys) { |
|
288 | 285 | $return = null; |
289 | 286 | foreach ($keys as $key) { |
290 | 287 | $result = $this->deleteItem($key); |
@@ -301,8 +298,7 @@ discard block |
||
301 | 298 | * @return bool |
302 | 299 | * @throws PhpfastcacheInvalidArgumentException |
303 | 300 | */ |
304 | - public function deleteItem($key) |
|
305 | - { |
|
301 | + public function deleteItem($key) { |
|
306 | 302 | $item = $this->getItem($key); |
307 | 303 | if ($item->isHit() && $this->driverDelete($item)) { |
308 | 304 | $item->setHit(false); |
@@ -339,11 +335,11 @@ discard block |
||
339 | 335 | * @return CacheItemInterface |
340 | 336 | * @throws RuntimeException |
341 | 337 | */ |
342 | - public function saveDeferred(CacheItemInterface $item) |
|
343 | - { |
|
338 | + public function saveDeferred(CacheItemInterface $item) { |
|
344 | 339 | if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
345 | 340 | $this->itemInstances[$item->getKey()] = $item; |
346 | - } else { |
|
341 | + } |
|
342 | + else { |
|
347 | 343 | if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
348 | 344 | throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
349 | 345 | } |
@@ -363,8 +359,7 @@ discard block |
||
363 | 359 | * @return bool |
364 | 360 | * @throws PhpfastcacheInvalidArgumentException |
365 | 361 | */ |
366 | - public function commit() |
|
367 | - { |
|
362 | + public function commit() { |
|
368 | 363 | /** |
369 | 364 | * @eventName CacheCommitItem |
370 | 365 | * @param $this ExtendedCacheItemPoolInterface |
@@ -391,8 +386,7 @@ discard block |
||
391 | 386 | * @throws PhpfastcacheLogicException |
392 | 387 | * @throws \ReflectionException |
393 | 388 | */ |
394 | - public function save(CacheItemInterface $item) |
|
395 | - { |
|
389 | + public function save(CacheItemInterface $item) { |
|
396 | 390 | /** |
397 | 391 | * @var ExtendedCacheItemInterface $item |
398 | 392 | * |
@@ -404,7 +398,8 @@ discard block |
||
404 | 398 | if($this->getConfig()->isUseStaticItemCaching()){ |
405 | 399 | $this->itemInstances[$item->getKey()] = $item; |
406 | 400 | } |
407 | - } else { |
|
401 | + } |
|
402 | + else { |
|
408 | 403 | if (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
409 | 404 | throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
410 | 405 | } |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | \implode( |
201 | 201 | ', ', |
202 | 202 | \array_map( |
203 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
203 | + static function(ExtendedCacheItemPoolInterface $pool) { |
|
204 | 204 | return \get_class($pool); |
205 | 205 | }, |
206 | 206 | $this->clusterPools |
@@ -210,9 +210,9 @@ discard block |
||
210 | 210 | ); |
211 | 211 | |
212 | 212 | $stats->setSize( |
213 | - (int)\array_sum( |
|
213 | + (int) \array_sum( |
|
214 | 214 | \array_map( |
215 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
215 | + static function(ExtendedCacheItemPoolInterface $pool) { |
|
216 | 216 | return $pool->getStats()->getSize(); |
217 | 217 | }, |
218 | 218 | $this->clusterPools |
@@ -221,8 +221,8 @@ discard block |
||
221 | 221 | ); |
222 | 222 | |
223 | 223 | $stats->setData( |
224 | - (int)\array_map( |
|
225 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
224 | + (int) \array_map( |
|
225 | + static function(ExtendedCacheItemPoolInterface $pool) { |
|
226 | 226 | return $pool->getStats()->getData(); |
227 | 227 | }, |
228 | 228 | $this->clusterPools |
@@ -16,9 +16,9 @@ discard block |
||
16 | 16 | namespace Phpfastcache\Cluster; |
17 | 17 | |
18 | 18 | use Phpfastcache\Cluster\Drivers\{FullReplication\FullReplicationCluster, |
19 | - MasterSlaveReplication\MasterSlaveReplicationCluster, |
|
20 | - RandomReplication\RandomReplicationCluster, |
|
21 | - SemiReplication\SemiReplicationCluster |
|
19 | + MasterSlaveReplication\MasterSlaveReplicationCluster, |
|
20 | + RandomReplication\RandomReplicationCluster, |
|
21 | + SemiReplication\SemiReplicationCluster |
|
22 | 22 | }; |
23 | 23 | use Phpfastcache\Config\ConfigurationOption; |
24 | 24 | use Phpfastcache\Core\{Item\ExtendedCacheItemInterface, Pool\DriverBaseTrait, Pool\ExtendedCacheItemPoolInterface}; |
@@ -39,196 +39,196 @@ discard block |
||
39 | 39 | */ |
40 | 40 | abstract class ClusterPoolAbstract implements ClusterPoolInterface |
41 | 41 | { |
42 | - use DriverBaseTrait; |
|
43 | - use ClusterPoolTrait { |
|
44 | - DriverBaseTrait::__construct as private __parentConstruct; |
|
45 | - } |
|
46 | - |
|
47 | - public const STRATEGY = [ |
|
48 | - AggregatorInterface::STRATEGY_FULL_REPLICATION => FullReplicationCluster::class, |
|
49 | - AggregatorInterface::STRATEGY_SEMI_REPLICATION => SemiReplicationCluster::class, |
|
50 | - AggregatorInterface::STRATEGY_MASTER_SLAVE => MasterSlaveReplicationCluster::class, |
|
51 | - AggregatorInterface::STRATEGY_RANDOM_REPLICATION => RandomReplicationCluster::class, |
|
52 | - ]; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var ExtendedCacheItemPoolInterface[] |
|
56 | - */ |
|
57 | - protected $clusterPools; |
|
58 | - |
|
59 | - /** |
|
60 | - * ClusterPoolAbstract constructor. |
|
61 | - * @param string $clusterName |
|
62 | - * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
63 | - * @throws PhpfastcacheInvalidArgumentException |
|
64 | - * @throws PhpfastcacheDriverCheckException |
|
65 | - * @throws PhpfastcacheDriverConnectException |
|
66 | - * @throws PhpfastcacheInvalidConfigurationException |
|
67 | - * @throws ReflectionException |
|
68 | - */ |
|
69 | - public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
70 | - { |
|
71 | - if (count($driverPools) < 2) { |
|
72 | - throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
|
73 | - } |
|
74 | - $this->clusterPools = $driverPools; |
|
75 | - $this->__parentConstruct(new ConfigurationOption(), $clusterName); |
|
76 | - $this->setEventManager(EventManager::getInstance()); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @inheritDoc |
|
81 | - */ |
|
82 | - public function getIO(): DriverIO |
|
83 | - { |
|
84 | - $IO = new DriverIO(); |
|
85 | - foreach ($this->clusterPools as $clusterPool) { |
|
86 | - $IO->setReadHit($IO->getReadHit() + $clusterPool->getIO()->getReadHit()) |
|
87 | - ->setReadMiss($IO->getReadMiss() + $clusterPool->getIO()->getReadMiss()) |
|
88 | - ->setWriteHit($IO->getWriteHit() + $clusterPool->getIO()->getWriteHit()); |
|
89 | - } |
|
90 | - return $IO; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @inheritDoc |
|
95 | - */ |
|
96 | - public function getClusterPools(): array |
|
97 | - { |
|
98 | - return $this->clusterPools; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @inheritDoc |
|
103 | - */ |
|
104 | - public function getItems(array $keys = []) |
|
105 | - { |
|
106 | - $items = []; |
|
107 | - |
|
108 | - foreach ($keys as $key) { |
|
109 | - $items[$key] = $this->getItem($key); |
|
110 | - } |
|
111 | - |
|
112 | - return $items; |
|
113 | - } |
|
114 | - /** |
|
115 | - * Shared method used by All Clusters |
|
116 | - */ |
|
117 | - |
|
118 | - /** |
|
119 | - * @inheritDoc |
|
120 | - */ |
|
121 | - public function deleteItems(array $keys) |
|
122 | - { |
|
123 | - $hasDeletedOnce = false; |
|
124 | - foreach ($this->clusterPools as $driverPool) { |
|
125 | - if ($result = $driverPool->deleteItems($keys)) { |
|
126 | - $hasDeletedOnce = $result; |
|
127 | - } |
|
128 | - } |
|
129 | - // Return true only if at least one backend confirmed the "clear" operation |
|
130 | - return $hasDeletedOnce; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @inheritDoc |
|
135 | - */ |
|
136 | - public function saveDeferred(CacheItemInterface $item) |
|
137 | - { |
|
138 | - /** @var ExtendedCacheItemInterface $item */ |
|
139 | - $hasSavedOnce = false; |
|
140 | - foreach ($this->clusterPools as $driverPool) { |
|
141 | - $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
142 | - if ($result = $driverPool->saveDeferred($poolItem)) { |
|
143 | - $hasSavedOnce = $result; |
|
144 | - } |
|
145 | - } |
|
146 | - // Return true only if at least one backend confirmed the "commit" operation |
|
147 | - return $hasSavedOnce; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param ExtendedCacheItemInterface $item |
|
152 | - * @param ExtendedCacheItemPoolInterface $driverPool |
|
153 | - * @return CacheItemInterface |
|
154 | - * @throws InvalidArgumentException |
|
155 | - */ |
|
156 | - protected function getStandardizedItem(ExtendedCacheItemInterface $item, ExtendedCacheItemPoolInterface $driverPool): CacheItemInterface |
|
157 | - { |
|
158 | - if (!$item->doesItemBelongToThatDriverBackend($driverPool)) { |
|
159 | - /** |
|
160 | - * Avoid infinite loop |
|
161 | - */ |
|
162 | - if ($driverPool === $this) { |
|
163 | - /** @var ExtendedCacheItemInterface $itemPool */ |
|
164 | - $itemClass = $driverPool->getClassNamespace() . '\\' . 'Item'; |
|
165 | - $itemPool = new $itemClass($this, $item->getKey()); |
|
166 | - $itemPool->setEventManager($this->getEventManager()) |
|
167 | - ->set($item->get()) |
|
168 | - ->setHit($item->isHit()) |
|
169 | - ->setTags($item->getTags()) |
|
170 | - ->expiresAt($item->getExpirationDate()) |
|
171 | - ->setDriver($driverPool); |
|
172 | - return $itemPool; |
|
173 | - } |
|
174 | - return $driverPool->getItem($item->getKey()) |
|
175 | - ->setEventManager($this->getEventManager()) |
|
176 | - ->set($item->get()) |
|
177 | - ->setHit($item->isHit()) |
|
178 | - ->setTags($item->getTags()) |
|
179 | - ->expiresAt($item->getExpirationDate()) |
|
180 | - ->setDriver($driverPool); |
|
181 | - } |
|
182 | - |
|
183 | - return $item->setEventManager($this->getEventManager()); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Interfaced methods that needs to be faked |
|
188 | - */ |
|
189 | - |
|
190 | - /** |
|
191 | - * @return DriverStatistic |
|
192 | - */ |
|
193 | - public function getStats(): DriverStatistic |
|
194 | - { |
|
195 | - $stats = new DriverStatistic(); |
|
196 | - $stats->setInfo( |
|
197 | - sprintf( |
|
198 | - 'Using %d pool(s): %s', |
|
199 | - \count($this->clusterPools), |
|
200 | - \implode( |
|
201 | - ', ', |
|
202 | - \array_map( |
|
203 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
204 | - return \get_class($pool); |
|
205 | - }, |
|
206 | - $this->clusterPools |
|
207 | - ) |
|
208 | - ) |
|
209 | - ) |
|
210 | - ); |
|
211 | - |
|
212 | - $stats->setSize( |
|
213 | - (int)\array_sum( |
|
214 | - \array_map( |
|
215 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
216 | - return $pool->getStats()->getSize(); |
|
217 | - }, |
|
218 | - $this->clusterPools |
|
219 | - ) |
|
220 | - ) |
|
221 | - ); |
|
222 | - |
|
223 | - $stats->setData( |
|
224 | - (int)\array_map( |
|
225 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
226 | - return $pool->getStats()->getData(); |
|
227 | - }, |
|
228 | - $this->clusterPools |
|
229 | - ) |
|
230 | - ); |
|
231 | - |
|
232 | - return $stats; |
|
233 | - } |
|
42 | + use DriverBaseTrait; |
|
43 | + use ClusterPoolTrait { |
|
44 | + DriverBaseTrait::__construct as private __parentConstruct; |
|
45 | + } |
|
46 | + |
|
47 | + public const STRATEGY = [ |
|
48 | + AggregatorInterface::STRATEGY_FULL_REPLICATION => FullReplicationCluster::class, |
|
49 | + AggregatorInterface::STRATEGY_SEMI_REPLICATION => SemiReplicationCluster::class, |
|
50 | + AggregatorInterface::STRATEGY_MASTER_SLAVE => MasterSlaveReplicationCluster::class, |
|
51 | + AggregatorInterface::STRATEGY_RANDOM_REPLICATION => RandomReplicationCluster::class, |
|
52 | + ]; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var ExtendedCacheItemPoolInterface[] |
|
56 | + */ |
|
57 | + protected $clusterPools; |
|
58 | + |
|
59 | + /** |
|
60 | + * ClusterPoolAbstract constructor. |
|
61 | + * @param string $clusterName |
|
62 | + * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
63 | + * @throws PhpfastcacheInvalidArgumentException |
|
64 | + * @throws PhpfastcacheDriverCheckException |
|
65 | + * @throws PhpfastcacheDriverConnectException |
|
66 | + * @throws PhpfastcacheInvalidConfigurationException |
|
67 | + * @throws ReflectionException |
|
68 | + */ |
|
69 | + public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
70 | + { |
|
71 | + if (count($driverPools) < 2) { |
|
72 | + throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
|
73 | + } |
|
74 | + $this->clusterPools = $driverPools; |
|
75 | + $this->__parentConstruct(new ConfigurationOption(), $clusterName); |
|
76 | + $this->setEventManager(EventManager::getInstance()); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @inheritDoc |
|
81 | + */ |
|
82 | + public function getIO(): DriverIO |
|
83 | + { |
|
84 | + $IO = new DriverIO(); |
|
85 | + foreach ($this->clusterPools as $clusterPool) { |
|
86 | + $IO->setReadHit($IO->getReadHit() + $clusterPool->getIO()->getReadHit()) |
|
87 | + ->setReadMiss($IO->getReadMiss() + $clusterPool->getIO()->getReadMiss()) |
|
88 | + ->setWriteHit($IO->getWriteHit() + $clusterPool->getIO()->getWriteHit()); |
|
89 | + } |
|
90 | + return $IO; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @inheritDoc |
|
95 | + */ |
|
96 | + public function getClusterPools(): array |
|
97 | + { |
|
98 | + return $this->clusterPools; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @inheritDoc |
|
103 | + */ |
|
104 | + public function getItems(array $keys = []) |
|
105 | + { |
|
106 | + $items = []; |
|
107 | + |
|
108 | + foreach ($keys as $key) { |
|
109 | + $items[$key] = $this->getItem($key); |
|
110 | + } |
|
111 | + |
|
112 | + return $items; |
|
113 | + } |
|
114 | + /** |
|
115 | + * Shared method used by All Clusters |
|
116 | + */ |
|
117 | + |
|
118 | + /** |
|
119 | + * @inheritDoc |
|
120 | + */ |
|
121 | + public function deleteItems(array $keys) |
|
122 | + { |
|
123 | + $hasDeletedOnce = false; |
|
124 | + foreach ($this->clusterPools as $driverPool) { |
|
125 | + if ($result = $driverPool->deleteItems($keys)) { |
|
126 | + $hasDeletedOnce = $result; |
|
127 | + } |
|
128 | + } |
|
129 | + // Return true only if at least one backend confirmed the "clear" operation |
|
130 | + return $hasDeletedOnce; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @inheritDoc |
|
135 | + */ |
|
136 | + public function saveDeferred(CacheItemInterface $item) |
|
137 | + { |
|
138 | + /** @var ExtendedCacheItemInterface $item */ |
|
139 | + $hasSavedOnce = false; |
|
140 | + foreach ($this->clusterPools as $driverPool) { |
|
141 | + $poolItem = $this->getStandardizedItem($item, $driverPool); |
|
142 | + if ($result = $driverPool->saveDeferred($poolItem)) { |
|
143 | + $hasSavedOnce = $result; |
|
144 | + } |
|
145 | + } |
|
146 | + // Return true only if at least one backend confirmed the "commit" operation |
|
147 | + return $hasSavedOnce; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param ExtendedCacheItemInterface $item |
|
152 | + * @param ExtendedCacheItemPoolInterface $driverPool |
|
153 | + * @return CacheItemInterface |
|
154 | + * @throws InvalidArgumentException |
|
155 | + */ |
|
156 | + protected function getStandardizedItem(ExtendedCacheItemInterface $item, ExtendedCacheItemPoolInterface $driverPool): CacheItemInterface |
|
157 | + { |
|
158 | + if (!$item->doesItemBelongToThatDriverBackend($driverPool)) { |
|
159 | + /** |
|
160 | + * Avoid infinite loop |
|
161 | + */ |
|
162 | + if ($driverPool === $this) { |
|
163 | + /** @var ExtendedCacheItemInterface $itemPool */ |
|
164 | + $itemClass = $driverPool->getClassNamespace() . '\\' . 'Item'; |
|
165 | + $itemPool = new $itemClass($this, $item->getKey()); |
|
166 | + $itemPool->setEventManager($this->getEventManager()) |
|
167 | + ->set($item->get()) |
|
168 | + ->setHit($item->isHit()) |
|
169 | + ->setTags($item->getTags()) |
|
170 | + ->expiresAt($item->getExpirationDate()) |
|
171 | + ->setDriver($driverPool); |
|
172 | + return $itemPool; |
|
173 | + } |
|
174 | + return $driverPool->getItem($item->getKey()) |
|
175 | + ->setEventManager($this->getEventManager()) |
|
176 | + ->set($item->get()) |
|
177 | + ->setHit($item->isHit()) |
|
178 | + ->setTags($item->getTags()) |
|
179 | + ->expiresAt($item->getExpirationDate()) |
|
180 | + ->setDriver($driverPool); |
|
181 | + } |
|
182 | + |
|
183 | + return $item->setEventManager($this->getEventManager()); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Interfaced methods that needs to be faked |
|
188 | + */ |
|
189 | + |
|
190 | + /** |
|
191 | + * @return DriverStatistic |
|
192 | + */ |
|
193 | + public function getStats(): DriverStatistic |
|
194 | + { |
|
195 | + $stats = new DriverStatistic(); |
|
196 | + $stats->setInfo( |
|
197 | + sprintf( |
|
198 | + 'Using %d pool(s): %s', |
|
199 | + \count($this->clusterPools), |
|
200 | + \implode( |
|
201 | + ', ', |
|
202 | + \array_map( |
|
203 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
204 | + return \get_class($pool); |
|
205 | + }, |
|
206 | + $this->clusterPools |
|
207 | + ) |
|
208 | + ) |
|
209 | + ) |
|
210 | + ); |
|
211 | + |
|
212 | + $stats->setSize( |
|
213 | + (int)\array_sum( |
|
214 | + \array_map( |
|
215 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
216 | + return $pool->getStats()->getSize(); |
|
217 | + }, |
|
218 | + $this->clusterPools |
|
219 | + ) |
|
220 | + ) |
|
221 | + ); |
|
222 | + |
|
223 | + $stats->setData( |
|
224 | + (int)\array_map( |
|
225 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
226 | + return $pool->getStats()->getData(); |
|
227 | + }, |
|
228 | + $this->clusterPools |
|
229 | + ) |
|
230 | + ); |
|
231 | + |
|
232 | + return $stats; |
|
233 | + } |
|
234 | 234 | } |
@@ -66,8 +66,7 @@ discard block |
||
66 | 66 | * @throws PhpfastcacheInvalidConfigurationException |
67 | 67 | * @throws ReflectionException |
68 | 68 | */ |
69 | - public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
70 | - { |
|
69 | + public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) { |
|
71 | 70 | if (count($driverPools) < 2) { |
72 | 71 | throw new PhpfastcacheInvalidArgumentException('A cluster requires at least two pools to be working.'); |
73 | 72 | } |
@@ -101,8 +100,7 @@ discard block |
||
101 | 100 | /** |
102 | 101 | * @inheritDoc |
103 | 102 | */ |
104 | - public function getItems(array $keys = []) |
|
105 | - { |
|
103 | + public function getItems(array $keys = []) { |
|
106 | 104 | $items = []; |
107 | 105 | |
108 | 106 | foreach ($keys as $key) { |
@@ -118,8 +116,7 @@ discard block |
||
118 | 116 | /** |
119 | 117 | * @inheritDoc |
120 | 118 | */ |
121 | - public function deleteItems(array $keys) |
|
122 | - { |
|
119 | + public function deleteItems(array $keys) { |
|
123 | 120 | $hasDeletedOnce = false; |
124 | 121 | foreach ($this->clusterPools as $driverPool) { |
125 | 122 | if ($result = $driverPool->deleteItems($keys)) { |
@@ -133,8 +130,7 @@ discard block |
||
133 | 130 | /** |
134 | 131 | * @inheritDoc |
135 | 132 | */ |
136 | - public function saveDeferred(CacheItemInterface $item) |
|
137 | - { |
|
133 | + public function saveDeferred(CacheItemInterface $item) { |
|
138 | 134 | /** @var ExtendedCacheItemInterface $item */ |
139 | 135 | $hasSavedOnce = false; |
140 | 136 | foreach ($this->clusterPools as $driverPool) { |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | { |
60 | 60 | return $this->getStandardizedItem( |
61 | 61 | $this->makeOperation( |
62 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
62 | + static function(ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
63 | 63 | return $pool->getItem($key); |
64 | 64 | } |
65 | 65 | ) ?? new Item($this, $key), |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | public function hasItem($key) |
113 | 113 | { |
114 | 114 | return $this->makeOperation( |
115 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
115 | + static function(ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
116 | 116 | return $pool->hasItem($key); |
117 | 117 | } |
118 | 118 | ); |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | public function clear() |
125 | 125 | { |
126 | 126 | return $this->makeOperation( |
127 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
127 | + static function(ExtendedCacheItemPoolInterface $pool) { |
|
128 | 128 | return $pool->clear(); |
129 | 129 | } |
130 | 130 | ); |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | public function deleteItem($key) |
137 | 137 | { |
138 | 138 | return $this->makeOperation( |
139 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
139 | + static function(ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
140 | 140 | return $pool->deleteItem($key); |
141 | 141 | } |
142 | 142 | ); |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | public function save(CacheItemInterface $item) |
149 | 149 | { |
150 | 150 | return $this->makeOperation( |
151 | - function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
151 | + function(ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
152 | 152 | $item->setHit(true); |
153 | 153 | return $pool->save($this->getStandardizedItem($item, $pool)); |
154 | 154 | } |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | public function commit() |
163 | 163 | { |
164 | 164 | return $this->makeOperation( |
165 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
165 | + static function(ExtendedCacheItemPoolInterface $pool) { |
|
166 | 166 | return $pool->commit(); |
167 | 167 | } |
168 | 168 | ); |
@@ -33,138 +33,138 @@ |
||
33 | 33 | */ |
34 | 34 | class MasterSlaveReplicationCluster extends ClusterPoolAbstract |
35 | 35 | { |
36 | - /** |
|
37 | - * MasterSlaveReplicationCluster constructor. |
|
38 | - * @param string $clusterName |
|
39 | - * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
40 | - * @throws PhpfastcacheInvalidArgumentException |
|
41 | - * @throws PhpfastcacheDriverCheckException |
|
42 | - * @throws PhpfastcacheDriverConnectException |
|
43 | - * @throws PhpfastcacheInvalidConfigurationException |
|
44 | - * @throws ReflectionException |
|
45 | - */ |
|
46 | - public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
47 | - { |
|
48 | - if (\count($driverPools) !== 2) { |
|
49 | - throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
50 | - } |
|
51 | - |
|
52 | - parent::__construct($clusterName, ...$driverPools); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @inheritDoc |
|
57 | - */ |
|
58 | - public function getItem($key) |
|
59 | - { |
|
60 | - return $this->getStandardizedItem( |
|
61 | - $this->makeOperation( |
|
62 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
63 | - return $pool->getItem($key); |
|
64 | - } |
|
65 | - ) ?? new Item($this, $key), |
|
66 | - $this |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param callable $operation |
|
72 | - * @return mixed |
|
73 | - * @throws PhpfastcacheReplicationException |
|
74 | - */ |
|
75 | - protected function makeOperation(callable $operation) |
|
76 | - { |
|
77 | - try { |
|
78 | - return $operation($this->getMasterPool()); |
|
79 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
80 | - try { |
|
81 | - $this->eventManager->dispatch( |
|
82 | - 'CacheReplicationSlaveFallback', |
|
83 | - $this, |
|
84 | - \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
85 | - ); |
|
86 | - return $operation($this->getSlavePool()); |
|
87 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
88 | - throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return ExtendedCacheItemPoolInterface |
|
95 | - */ |
|
96 | - protected function getMasterPool(): ExtendedCacheItemPoolInterface |
|
97 | - { |
|
98 | - return $this->clusterPools[0]; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return ExtendedCacheItemPoolInterface |
|
103 | - */ |
|
104 | - protected function getSlavePool(): ExtendedCacheItemPoolInterface |
|
105 | - { |
|
106 | - return $this->clusterPools[1]; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @inheritDoc |
|
111 | - */ |
|
112 | - public function hasItem($key) |
|
113 | - { |
|
114 | - return $this->makeOperation( |
|
115 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
116 | - return $pool->hasItem($key); |
|
117 | - } |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @inheritDoc |
|
123 | - */ |
|
124 | - public function clear() |
|
125 | - { |
|
126 | - return $this->makeOperation( |
|
127 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
128 | - return $pool->clear(); |
|
129 | - } |
|
130 | - ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @inheritDoc |
|
135 | - */ |
|
136 | - public function deleteItem($key) |
|
137 | - { |
|
138 | - return $this->makeOperation( |
|
139 | - static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
140 | - return $pool->deleteItem($key); |
|
141 | - } |
|
142 | - ); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @inheritDoc |
|
147 | - */ |
|
148 | - public function save(CacheItemInterface $item) |
|
149 | - { |
|
150 | - return $this->makeOperation( |
|
151 | - function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
152 | - $item->setHit(true); |
|
153 | - return $pool->save($this->getStandardizedItem($item, $pool)); |
|
154 | - } |
|
155 | - ); |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * @inheritDoc |
|
161 | - */ |
|
162 | - public function commit() |
|
163 | - { |
|
164 | - return $this->makeOperation( |
|
165 | - static function (ExtendedCacheItemPoolInterface $pool) { |
|
166 | - return $pool->commit(); |
|
167 | - } |
|
168 | - ); |
|
169 | - } |
|
36 | + /** |
|
37 | + * MasterSlaveReplicationCluster constructor. |
|
38 | + * @param string $clusterName |
|
39 | + * @param ExtendedCacheItemPoolInterface ...$driverPools |
|
40 | + * @throws PhpfastcacheInvalidArgumentException |
|
41 | + * @throws PhpfastcacheDriverCheckException |
|
42 | + * @throws PhpfastcacheDriverConnectException |
|
43 | + * @throws PhpfastcacheInvalidConfigurationException |
|
44 | + * @throws ReflectionException |
|
45 | + */ |
|
46 | + public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
47 | + { |
|
48 | + if (\count($driverPools) !== 2) { |
|
49 | + throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
|
50 | + } |
|
51 | + |
|
52 | + parent::__construct($clusterName, ...$driverPools); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @inheritDoc |
|
57 | + */ |
|
58 | + public function getItem($key) |
|
59 | + { |
|
60 | + return $this->getStandardizedItem( |
|
61 | + $this->makeOperation( |
|
62 | + static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
63 | + return $pool->getItem($key); |
|
64 | + } |
|
65 | + ) ?? new Item($this, $key), |
|
66 | + $this |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param callable $operation |
|
72 | + * @return mixed |
|
73 | + * @throws PhpfastcacheReplicationException |
|
74 | + */ |
|
75 | + protected function makeOperation(callable $operation) |
|
76 | + { |
|
77 | + try { |
|
78 | + return $operation($this->getMasterPool()); |
|
79 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
80 | + try { |
|
81 | + $this->eventManager->dispatch( |
|
82 | + 'CacheReplicationSlaveFallback', |
|
83 | + $this, |
|
84 | + \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
|
85 | + ); |
|
86 | + return $operation($this->getSlavePool()); |
|
87 | + } catch (PhpfastcacheExceptionInterface $e) { |
|
88 | + throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return ExtendedCacheItemPoolInterface |
|
95 | + */ |
|
96 | + protected function getMasterPool(): ExtendedCacheItemPoolInterface |
|
97 | + { |
|
98 | + return $this->clusterPools[0]; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return ExtendedCacheItemPoolInterface |
|
103 | + */ |
|
104 | + protected function getSlavePool(): ExtendedCacheItemPoolInterface |
|
105 | + { |
|
106 | + return $this->clusterPools[1]; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @inheritDoc |
|
111 | + */ |
|
112 | + public function hasItem($key) |
|
113 | + { |
|
114 | + return $this->makeOperation( |
|
115 | + static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
116 | + return $pool->hasItem($key); |
|
117 | + } |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @inheritDoc |
|
123 | + */ |
|
124 | + public function clear() |
|
125 | + { |
|
126 | + return $this->makeOperation( |
|
127 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
128 | + return $pool->clear(); |
|
129 | + } |
|
130 | + ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @inheritDoc |
|
135 | + */ |
|
136 | + public function deleteItem($key) |
|
137 | + { |
|
138 | + return $this->makeOperation( |
|
139 | + static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
|
140 | + return $pool->deleteItem($key); |
|
141 | + } |
|
142 | + ); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @inheritDoc |
|
147 | + */ |
|
148 | + public function save(CacheItemInterface $item) |
|
149 | + { |
|
150 | + return $this->makeOperation( |
|
151 | + function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
|
152 | + $item->setHit(true); |
|
153 | + return $pool->save($this->getStandardizedItem($item, $pool)); |
|
154 | + } |
|
155 | + ); |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * @inheritDoc |
|
161 | + */ |
|
162 | + public function commit() |
|
163 | + { |
|
164 | + return $this->makeOperation( |
|
165 | + static function (ExtendedCacheItemPoolInterface $pool) { |
|
166 | + return $pool->commit(); |
|
167 | + } |
|
168 | + ); |
|
169 | + } |
|
170 | 170 | } |
@@ -43,8 +43,7 @@ discard block |
||
43 | 43 | * @throws PhpfastcacheInvalidConfigurationException |
44 | 44 | * @throws ReflectionException |
45 | 45 | */ |
46 | - public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) |
|
47 | - { |
|
46 | + public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools) { |
|
48 | 47 | if (\count($driverPools) !== 2) { |
49 | 48 | throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.'); |
50 | 49 | } |
@@ -55,8 +54,7 @@ discard block |
||
55 | 54 | /** |
56 | 55 | * @inheritDoc |
57 | 56 | */ |
58 | - public function getItem($key) |
|
59 | - { |
|
57 | + public function getItem($key) { |
|
60 | 58 | return $this->getStandardizedItem( |
61 | 59 | $this->makeOperation( |
62 | 60 | static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
@@ -72,11 +70,11 @@ discard block |
||
72 | 70 | * @return mixed |
73 | 71 | * @throws PhpfastcacheReplicationException |
74 | 72 | */ |
75 | - protected function makeOperation(callable $operation) |
|
76 | - { |
|
73 | + protected function makeOperation(callable $operation) { |
|
77 | 74 | try { |
78 | 75 | return $operation($this->getMasterPool()); |
79 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
76 | + } |
|
77 | + catch (PhpfastcacheExceptionInterface $e) { |
|
80 | 78 | try { |
81 | 79 | $this->eventManager->dispatch( |
82 | 80 | 'CacheReplicationSlaveFallback', |
@@ -84,7 +82,8 @@ discard block |
||
84 | 82 | \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] |
85 | 83 | ); |
86 | 84 | return $operation($this->getSlavePool()); |
87 | - } catch (PhpfastcacheExceptionInterface $e) { |
|
85 | + } |
|
86 | + catch (PhpfastcacheExceptionInterface $e) { |
|
88 | 87 | throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !'); |
89 | 88 | } |
90 | 89 | } |
@@ -109,8 +108,7 @@ discard block |
||
109 | 108 | /** |
110 | 109 | * @inheritDoc |
111 | 110 | */ |
112 | - public function hasItem($key) |
|
113 | - { |
|
111 | + public function hasItem($key) { |
|
114 | 112 | return $this->makeOperation( |
115 | 113 | static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
116 | 114 | return $pool->hasItem($key); |
@@ -121,8 +119,7 @@ discard block |
||
121 | 119 | /** |
122 | 120 | * @inheritDoc |
123 | 121 | */ |
124 | - public function clear() |
|
125 | - { |
|
122 | + public function clear() { |
|
126 | 123 | return $this->makeOperation( |
127 | 124 | static function (ExtendedCacheItemPoolInterface $pool) { |
128 | 125 | return $pool->clear(); |
@@ -133,8 +130,7 @@ discard block |
||
133 | 130 | /** |
134 | 131 | * @inheritDoc |
135 | 132 | */ |
136 | - public function deleteItem($key) |
|
137 | - { |
|
133 | + public function deleteItem($key) { |
|
138 | 134 | return $this->makeOperation( |
139 | 135 | static function (ExtendedCacheItemPoolInterface $pool) use ($key) { |
140 | 136 | return $pool->deleteItem($key); |
@@ -145,8 +141,7 @@ discard block |
||
145 | 141 | /** |
146 | 142 | * @inheritDoc |
147 | 143 | */ |
148 | - public function save(CacheItemInterface $item) |
|
149 | - { |
|
144 | + public function save(CacheItemInterface $item) { |
|
150 | 145 | return $this->makeOperation( |
151 | 146 | function (ExtendedCacheItemPoolInterface $pool) use ($item) { |
152 | 147 | $item->setHit(true); |
@@ -159,8 +154,7 @@ discard block |
||
159 | 154 | /** |
160 | 155 | * @inheritDoc |
161 | 156 | */ |
162 | - public function commit() |
|
163 | - { |
|
157 | + public function commit() { |
|
164 | 158 | return $this->makeOperation( |
165 | 159 | static function (ExtendedCacheItemPoolInterface $pool) { |
166 | 160 | return $pool->commit(); |
@@ -98,7 +98,7 @@ |
||
98 | 98 | public function __construct(...$args) |
99 | 99 | { |
100 | 100 | parent::__construct(...$args); |
101 | - $array =& $this->getArray(); |
|
101 | + $array = & $this->getArray(); |
|
102 | 102 | |
103 | 103 | /** |
104 | 104 | * Detect unwanted keys and throw an exception. |
@@ -29,361 +29,361 @@ |
||
29 | 29 | */ |
30 | 30 | class ConfigurationOption extends ArrayObject implements ConfigurationOptionInterface |
31 | 31 | { |
32 | - /** |
|
33 | - * @var bool |
|
34 | - */ |
|
35 | - protected $itemDetailedDate = false; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var bool |
|
39 | - */ |
|
40 | - protected $autoTmpFallback = false; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - protected $defaultTtl = 900; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var string|Callable |
|
49 | - */ |
|
50 | - protected $defaultKeyHashFunction = 'md5'; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var string|Callable |
|
54 | - */ |
|
55 | - protected $defaultFileNameHashFunction = 'md5'; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var int |
|
59 | - */ |
|
60 | - protected $defaultChmod = 0777; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var string |
|
64 | - */ |
|
65 | - protected $path = ''; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var int |
|
69 | - */ |
|
70 | - protected $limitedMemoryByObject = 4096; |
|
71 | - |
|
72 | - /** |
|
73 | - * @var bool |
|
74 | - */ |
|
75 | - protected $compressData = false; |
|
76 | - |
|
77 | - /** |
|
78 | - * @var bool |
|
79 | - */ |
|
80 | - protected $preventCacheSlams = false; |
|
81 | - |
|
82 | - /** |
|
83 | - * @var int |
|
84 | - */ |
|
85 | - protected $cacheSlamsTimeout = 15; |
|
86 | - |
|
87 | - /** |
|
88 | - * @var bool |
|
89 | - */ |
|
90 | - protected $useStaticItemCaching = true; |
|
91 | - |
|
92 | - /** |
|
93 | - * @param $args |
|
94 | - * ArrayObject constructor. |
|
95 | - * @throws PhpfastcacheInvalidConfigurationException |
|
96 | - * @throws ReflectionException |
|
97 | - */ |
|
98 | - public function __construct(...$args) |
|
99 | - { |
|
100 | - parent::__construct(...$args); |
|
101 | - $array =& $this->getArray(); |
|
102 | - |
|
103 | - /** |
|
104 | - * Detect unwanted keys and throw an exception. |
|
105 | - * No more kidding now, it's 21th century. |
|
106 | - */ |
|
107 | - if (\array_diff_key($array, \get_object_vars($this))) { |
|
108 | - throw new PhpfastcacheInvalidConfigurationException( |
|
109 | - sprintf( |
|
110 | - 'Invalid option(s) for the config %s: %s', |
|
111 | - static::class, |
|
112 | - \implode(', ', \array_keys(\array_diff_key($array, \get_object_vars($this)))) |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - foreach (\get_object_vars($this) as $property => $value) { |
|
118 | - if (\array_key_exists($property, $array)) { |
|
119 | - $this->$property = &$array[$property]; |
|
120 | - } else { |
|
121 | - $array[$property] = &$this->$property; |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - foreach (\get_class_methods($this) as $method) { |
|
126 | - if (\strpos($method, 'set') === 0) { |
|
127 | - $value = null; |
|
128 | - try { |
|
129 | - /** |
|
130 | - * We use property instead of getter |
|
131 | - * because of is/get conditions and |
|
132 | - * to allow us to retrieve the value |
|
133 | - * in catch statement block |
|
134 | - */ |
|
135 | - $value = $this->{\lcfirst(\substr($method, 3))}; |
|
136 | - $this->{$method}($value); |
|
137 | - } catch (TypeError $e) { |
|
138 | - $typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
|
139 | - $reflectionMethod = new ReflectionMethod($this, $method); |
|
140 | - $parameter = $reflectionMethod->getParameters()[0] ?? null; |
|
141 | - $typeHintExpected = ($parameter instanceof ReflectionParameter ? ($parameter->getType()->getName() === 'object' ? $parameter->getClass() : $parameter->getType( |
|
142 | - )->getName()) : 'Unknown type'); |
|
143 | - |
|
144 | - throw new PhpfastcacheInvalidConfigurationException( |
|
145 | - \sprintf( |
|
146 | - 'Invalid type hint found for "%s", expected "%s" got "%s"', |
|
147 | - \lcfirst(\substr($method, 3)), |
|
148 | - $typeHintExpected, |
|
149 | - $typeHintGot |
|
150 | - ) |
|
151 | - ); |
|
152 | - } |
|
153 | - } |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param string $optionName |
|
159 | - * @return mixed|null |
|
160 | - */ |
|
161 | - public function isValidOption(string $optionName) |
|
162 | - { |
|
163 | - return property_exists($this, $optionName); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - public function isItemDetailedDate(): bool |
|
170 | - { |
|
171 | - return $this->itemDetailedDate; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param bool $itemDetailedDate |
|
176 | - * @return ConfigurationOption |
|
177 | - */ |
|
178 | - public function setItemDetailedDate(bool $itemDetailedDate): self |
|
179 | - { |
|
180 | - $this->itemDetailedDate = $itemDetailedDate; |
|
181 | - return $this; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * @return bool |
|
186 | - */ |
|
187 | - public function isAutoTmpFallback(): bool |
|
188 | - { |
|
189 | - return $this->autoTmpFallback; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @param bool $autoTmpFallback |
|
194 | - * @return ConfigurationOption |
|
195 | - */ |
|
196 | - public function setAutoTmpFallback(bool $autoTmpFallback): self |
|
197 | - { |
|
198 | - $this->autoTmpFallback = $autoTmpFallback; |
|
199 | - return $this; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @return int |
|
204 | - */ |
|
205 | - public function getDefaultTtl(): int |
|
206 | - { |
|
207 | - return $this->defaultTtl; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @param int $defaultTtl |
|
212 | - * @return ConfigurationOption |
|
213 | - */ |
|
214 | - public function setDefaultTtl(int $defaultTtl): self |
|
215 | - { |
|
216 | - $this->defaultTtl = $defaultTtl; |
|
217 | - return $this; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @return Callable|string |
|
222 | - */ |
|
223 | - public function getDefaultKeyHashFunction() |
|
224 | - { |
|
225 | - return $this->defaultKeyHashFunction; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @param Callable|string $defaultKeyHashFunction |
|
230 | - * @return ConfigurationOption |
|
231 | - * @throws PhpfastcacheInvalidConfigurationException |
|
232 | - */ |
|
233 | - public function setDefaultKeyHashFunction($defaultKeyHashFunction): self |
|
234 | - { |
|
235 | - if ($defaultKeyHashFunction && !\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) { |
|
236 | - throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string'); |
|
237 | - } |
|
238 | - $this->defaultKeyHashFunction = $defaultKeyHashFunction; |
|
239 | - return $this; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * @return Callable|string |
|
244 | - */ |
|
245 | - public function getDefaultFileNameHashFunction() |
|
246 | - { |
|
247 | - return $this->defaultFileNameHashFunction; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * @param Callable|string $defaultFileNameHashFunction |
|
252 | - * @return ConfigurationOption |
|
253 | - * @throws PhpfastcacheInvalidConfigurationException |
|
254 | - */ |
|
255 | - public function setDefaultFileNameHashFunction($defaultFileNameHashFunction): self |
|
256 | - { |
|
257 | - if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) { |
|
258 | - throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
|
259 | - } |
|
260 | - $this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
|
261 | - return $this; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @return int |
|
266 | - */ |
|
267 | - public function getDefaultChmod(): int |
|
268 | - { |
|
269 | - return $this->defaultChmod; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @param int $defaultChmod |
|
274 | - * @return ConfigurationOption |
|
275 | - */ |
|
276 | - public function setDefaultChmod(int $defaultChmod): self |
|
277 | - { |
|
278 | - $this->defaultChmod = $defaultChmod; |
|
279 | - return $this; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @return string |
|
284 | - */ |
|
285 | - public function getPath(): string |
|
286 | - { |
|
287 | - return $this->path; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * @param string $path |
|
292 | - * @return ConfigurationOption |
|
293 | - */ |
|
294 | - public function setPath(string $path): self |
|
295 | - { |
|
296 | - $this->path = $path; |
|
297 | - return $this; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * @return int |
|
302 | - */ |
|
303 | - public function getLimitedMemoryByObject(): int |
|
304 | - { |
|
305 | - return $this->limitedMemoryByObject; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * @param int $limitedMemoryByObject |
|
310 | - * @return ConfigurationOption |
|
311 | - */ |
|
312 | - public function setLimitedMemoryByObject(int $limitedMemoryByObject): self |
|
313 | - { |
|
314 | - $this->limitedMemoryByObject = $limitedMemoryByObject; |
|
315 | - return $this; |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * @return bool |
|
320 | - */ |
|
321 | - public function isCompressData(): bool |
|
322 | - { |
|
323 | - return $this->compressData; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * @param bool $compressData |
|
328 | - * @return ConfigurationOption |
|
329 | - */ |
|
330 | - public function setCompressData(bool $compressData): self |
|
331 | - { |
|
332 | - $this->compressData = $compressData; |
|
333 | - return $this; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * @return bool |
|
338 | - */ |
|
339 | - public function isPreventCacheSlams(): bool |
|
340 | - { |
|
341 | - return $this->preventCacheSlams; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * @param bool $preventCacheSlams |
|
346 | - * @return ConfigurationOption |
|
347 | - */ |
|
348 | - public function setPreventCacheSlams(bool $preventCacheSlams): self |
|
349 | - { |
|
350 | - $this->preventCacheSlams = $preventCacheSlams; |
|
351 | - return $this; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * @return int |
|
356 | - */ |
|
357 | - public function getCacheSlamsTimeout(): int |
|
358 | - { |
|
359 | - return $this->cacheSlamsTimeout; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @param int $cacheSlamsTimeout |
|
364 | - * @return ConfigurationOption |
|
365 | - */ |
|
366 | - public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self |
|
367 | - { |
|
368 | - $this->cacheSlamsTimeout = $cacheSlamsTimeout; |
|
369 | - return $this; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @return bool |
|
374 | - */ |
|
375 | - public function isUseStaticItemCaching(): bool |
|
376 | - { |
|
377 | - return $this->useStaticItemCaching; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * @param bool $useStaticItemCaching |
|
382 | - * @return ConfigurationOption |
|
383 | - */ |
|
384 | - public function setUseStaticItemCaching(bool $useStaticItemCaching): self |
|
385 | - { |
|
386 | - $this->useStaticItemCaching = $useStaticItemCaching; |
|
387 | - return $this; |
|
388 | - } |
|
32 | + /** |
|
33 | + * @var bool |
|
34 | + */ |
|
35 | + protected $itemDetailedDate = false; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var bool |
|
39 | + */ |
|
40 | + protected $autoTmpFallback = false; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + protected $defaultTtl = 900; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var string|Callable |
|
49 | + */ |
|
50 | + protected $defaultKeyHashFunction = 'md5'; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var string|Callable |
|
54 | + */ |
|
55 | + protected $defaultFileNameHashFunction = 'md5'; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var int |
|
59 | + */ |
|
60 | + protected $defaultChmod = 0777; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var string |
|
64 | + */ |
|
65 | + protected $path = ''; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var int |
|
69 | + */ |
|
70 | + protected $limitedMemoryByObject = 4096; |
|
71 | + |
|
72 | + /** |
|
73 | + * @var bool |
|
74 | + */ |
|
75 | + protected $compressData = false; |
|
76 | + |
|
77 | + /** |
|
78 | + * @var bool |
|
79 | + */ |
|
80 | + protected $preventCacheSlams = false; |
|
81 | + |
|
82 | + /** |
|
83 | + * @var int |
|
84 | + */ |
|
85 | + protected $cacheSlamsTimeout = 15; |
|
86 | + |
|
87 | + /** |
|
88 | + * @var bool |
|
89 | + */ |
|
90 | + protected $useStaticItemCaching = true; |
|
91 | + |
|
92 | + /** |
|
93 | + * @param $args |
|
94 | + * ArrayObject constructor. |
|
95 | + * @throws PhpfastcacheInvalidConfigurationException |
|
96 | + * @throws ReflectionException |
|
97 | + */ |
|
98 | + public function __construct(...$args) |
|
99 | + { |
|
100 | + parent::__construct(...$args); |
|
101 | + $array =& $this->getArray(); |
|
102 | + |
|
103 | + /** |
|
104 | + * Detect unwanted keys and throw an exception. |
|
105 | + * No more kidding now, it's 21th century. |
|
106 | + */ |
|
107 | + if (\array_diff_key($array, \get_object_vars($this))) { |
|
108 | + throw new PhpfastcacheInvalidConfigurationException( |
|
109 | + sprintf( |
|
110 | + 'Invalid option(s) for the config %s: %s', |
|
111 | + static::class, |
|
112 | + \implode(', ', \array_keys(\array_diff_key($array, \get_object_vars($this)))) |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + foreach (\get_object_vars($this) as $property => $value) { |
|
118 | + if (\array_key_exists($property, $array)) { |
|
119 | + $this->$property = &$array[$property]; |
|
120 | + } else { |
|
121 | + $array[$property] = &$this->$property; |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + foreach (\get_class_methods($this) as $method) { |
|
126 | + if (\strpos($method, 'set') === 0) { |
|
127 | + $value = null; |
|
128 | + try { |
|
129 | + /** |
|
130 | + * We use property instead of getter |
|
131 | + * because of is/get conditions and |
|
132 | + * to allow us to retrieve the value |
|
133 | + * in catch statement block |
|
134 | + */ |
|
135 | + $value = $this->{\lcfirst(\substr($method, 3))}; |
|
136 | + $this->{$method}($value); |
|
137 | + } catch (TypeError $e) { |
|
138 | + $typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
|
139 | + $reflectionMethod = new ReflectionMethod($this, $method); |
|
140 | + $parameter = $reflectionMethod->getParameters()[0] ?? null; |
|
141 | + $typeHintExpected = ($parameter instanceof ReflectionParameter ? ($parameter->getType()->getName() === 'object' ? $parameter->getClass() : $parameter->getType( |
|
142 | + )->getName()) : 'Unknown type'); |
|
143 | + |
|
144 | + throw new PhpfastcacheInvalidConfigurationException( |
|
145 | + \sprintf( |
|
146 | + 'Invalid type hint found for "%s", expected "%s" got "%s"', |
|
147 | + \lcfirst(\substr($method, 3)), |
|
148 | + $typeHintExpected, |
|
149 | + $typeHintGot |
|
150 | + ) |
|
151 | + ); |
|
152 | + } |
|
153 | + } |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param string $optionName |
|
159 | + * @return mixed|null |
|
160 | + */ |
|
161 | + public function isValidOption(string $optionName) |
|
162 | + { |
|
163 | + return property_exists($this, $optionName); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + public function isItemDetailedDate(): bool |
|
170 | + { |
|
171 | + return $this->itemDetailedDate; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param bool $itemDetailedDate |
|
176 | + * @return ConfigurationOption |
|
177 | + */ |
|
178 | + public function setItemDetailedDate(bool $itemDetailedDate): self |
|
179 | + { |
|
180 | + $this->itemDetailedDate = $itemDetailedDate; |
|
181 | + return $this; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * @return bool |
|
186 | + */ |
|
187 | + public function isAutoTmpFallback(): bool |
|
188 | + { |
|
189 | + return $this->autoTmpFallback; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @param bool $autoTmpFallback |
|
194 | + * @return ConfigurationOption |
|
195 | + */ |
|
196 | + public function setAutoTmpFallback(bool $autoTmpFallback): self |
|
197 | + { |
|
198 | + $this->autoTmpFallback = $autoTmpFallback; |
|
199 | + return $this; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @return int |
|
204 | + */ |
|
205 | + public function getDefaultTtl(): int |
|
206 | + { |
|
207 | + return $this->defaultTtl; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @param int $defaultTtl |
|
212 | + * @return ConfigurationOption |
|
213 | + */ |
|
214 | + public function setDefaultTtl(int $defaultTtl): self |
|
215 | + { |
|
216 | + $this->defaultTtl = $defaultTtl; |
|
217 | + return $this; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @return Callable|string |
|
222 | + */ |
|
223 | + public function getDefaultKeyHashFunction() |
|
224 | + { |
|
225 | + return $this->defaultKeyHashFunction; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @param Callable|string $defaultKeyHashFunction |
|
230 | + * @return ConfigurationOption |
|
231 | + * @throws PhpfastcacheInvalidConfigurationException |
|
232 | + */ |
|
233 | + public function setDefaultKeyHashFunction($defaultKeyHashFunction): self |
|
234 | + { |
|
235 | + if ($defaultKeyHashFunction && !\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) { |
|
236 | + throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string'); |
|
237 | + } |
|
238 | + $this->defaultKeyHashFunction = $defaultKeyHashFunction; |
|
239 | + return $this; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * @return Callable|string |
|
244 | + */ |
|
245 | + public function getDefaultFileNameHashFunction() |
|
246 | + { |
|
247 | + return $this->defaultFileNameHashFunction; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * @param Callable|string $defaultFileNameHashFunction |
|
252 | + * @return ConfigurationOption |
|
253 | + * @throws PhpfastcacheInvalidConfigurationException |
|
254 | + */ |
|
255 | + public function setDefaultFileNameHashFunction($defaultFileNameHashFunction): self |
|
256 | + { |
|
257 | + if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) { |
|
258 | + throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string'); |
|
259 | + } |
|
260 | + $this->defaultFileNameHashFunction = $defaultFileNameHashFunction; |
|
261 | + return $this; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @return int |
|
266 | + */ |
|
267 | + public function getDefaultChmod(): int |
|
268 | + { |
|
269 | + return $this->defaultChmod; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @param int $defaultChmod |
|
274 | + * @return ConfigurationOption |
|
275 | + */ |
|
276 | + public function setDefaultChmod(int $defaultChmod): self |
|
277 | + { |
|
278 | + $this->defaultChmod = $defaultChmod; |
|
279 | + return $this; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @return string |
|
284 | + */ |
|
285 | + public function getPath(): string |
|
286 | + { |
|
287 | + return $this->path; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * @param string $path |
|
292 | + * @return ConfigurationOption |
|
293 | + */ |
|
294 | + public function setPath(string $path): self |
|
295 | + { |
|
296 | + $this->path = $path; |
|
297 | + return $this; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * @return int |
|
302 | + */ |
|
303 | + public function getLimitedMemoryByObject(): int |
|
304 | + { |
|
305 | + return $this->limitedMemoryByObject; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * @param int $limitedMemoryByObject |
|
310 | + * @return ConfigurationOption |
|
311 | + */ |
|
312 | + public function setLimitedMemoryByObject(int $limitedMemoryByObject): self |
|
313 | + { |
|
314 | + $this->limitedMemoryByObject = $limitedMemoryByObject; |
|
315 | + return $this; |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * @return bool |
|
320 | + */ |
|
321 | + public function isCompressData(): bool |
|
322 | + { |
|
323 | + return $this->compressData; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * @param bool $compressData |
|
328 | + * @return ConfigurationOption |
|
329 | + */ |
|
330 | + public function setCompressData(bool $compressData): self |
|
331 | + { |
|
332 | + $this->compressData = $compressData; |
|
333 | + return $this; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * @return bool |
|
338 | + */ |
|
339 | + public function isPreventCacheSlams(): bool |
|
340 | + { |
|
341 | + return $this->preventCacheSlams; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * @param bool $preventCacheSlams |
|
346 | + * @return ConfigurationOption |
|
347 | + */ |
|
348 | + public function setPreventCacheSlams(bool $preventCacheSlams): self |
|
349 | + { |
|
350 | + $this->preventCacheSlams = $preventCacheSlams; |
|
351 | + return $this; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * @return int |
|
356 | + */ |
|
357 | + public function getCacheSlamsTimeout(): int |
|
358 | + { |
|
359 | + return $this->cacheSlamsTimeout; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @param int $cacheSlamsTimeout |
|
364 | + * @return ConfigurationOption |
|
365 | + */ |
|
366 | + public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self |
|
367 | + { |
|
368 | + $this->cacheSlamsTimeout = $cacheSlamsTimeout; |
|
369 | + return $this; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @return bool |
|
374 | + */ |
|
375 | + public function isUseStaticItemCaching(): bool |
|
376 | + { |
|
377 | + return $this->useStaticItemCaching; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * @param bool $useStaticItemCaching |
|
382 | + * @return ConfigurationOption |
|
383 | + */ |
|
384 | + public function setUseStaticItemCaching(bool $useStaticItemCaching): self |
|
385 | + { |
|
386 | + $this->useStaticItemCaching = $useStaticItemCaching; |
|
387 | + return $this; |
|
388 | + } |
|
389 | 389 | } |
@@ -95,8 +95,7 @@ discard block |
||
95 | 95 | * @throws PhpfastcacheInvalidConfigurationException |
96 | 96 | * @throws ReflectionException |
97 | 97 | */ |
98 | - public function __construct(...$args) |
|
99 | - { |
|
98 | + public function __construct(...$args) { |
|
100 | 99 | parent::__construct(...$args); |
101 | 100 | $array =& $this->getArray(); |
102 | 101 | |
@@ -117,7 +116,8 @@ discard block |
||
117 | 116 | foreach (\get_object_vars($this) as $property => $value) { |
118 | 117 | if (\array_key_exists($property, $array)) { |
119 | 118 | $this->$property = &$array[$property]; |
120 | - } else { |
|
119 | + } |
|
120 | + else { |
|
121 | 121 | $array[$property] = &$this->$property; |
122 | 122 | } |
123 | 123 | } |
@@ -134,7 +134,8 @@ discard block |
||
134 | 134 | */ |
135 | 135 | $value = $this->{\lcfirst(\substr($method, 3))}; |
136 | 136 | $this->{$method}($value); |
137 | - } catch (TypeError $e) { |
|
137 | + } |
|
138 | + catch (TypeError $e) { |
|
138 | 139 | $typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value); |
139 | 140 | $reflectionMethod = new ReflectionMethod($this, $method); |
140 | 141 | $parameter = $reflectionMethod->getParameters()[0] ?? null; |
@@ -158,8 +159,7 @@ discard block |
||
158 | 159 | * @param string $optionName |
159 | 160 | * @return mixed|null |
160 | 161 | */ |
161 | - public function isValidOption(string $optionName) |
|
162 | - { |
|
162 | + public function isValidOption(string $optionName) { |
|
163 | 163 | return property_exists($this, $optionName); |
164 | 164 | } |
165 | 165 | |
@@ -220,8 +220,7 @@ discard block |
||
220 | 220 | /** |
221 | 221 | * @return Callable|string |
222 | 222 | */ |
223 | - public function getDefaultKeyHashFunction() |
|
224 | - { |
|
223 | + public function getDefaultKeyHashFunction() { |
|
225 | 224 | return $this->defaultKeyHashFunction; |
226 | 225 | } |
227 | 226 | |
@@ -242,8 +241,7 @@ discard block |
||
242 | 241 | /** |
243 | 242 | * @return Callable|string |
244 | 243 | */ |
245 | - public function getDefaultFileNameHashFunction() |
|
246 | - { |
|
244 | + public function getDefaultFileNameHashFunction() { |
|
247 | 245 | return $this->defaultFileNameHashFunction; |
248 | 246 | } |
249 | 247 |
@@ -68,7 +68,7 @@ |
||
68 | 68 | public function setDatabase(string $database): Config |
69 | 69 | { |
70 | 70 | /** @see https://docs.couchdb.org/en/latest/api/database/common.html#put--db */ |
71 | - if(\preg_match('#^[a-z][a-z0-9_\-+\$()/]+$#', $database)){ |
|
71 | + if (\preg_match('#^[a-z][a-z0-9_\-+\$()/]+$#', $database)) { |
|
72 | 72 | $this->database = $database; |
73 | 73 | return $this; |
74 | 74 | } |
@@ -21,169 +21,169 @@ |
||
21 | 21 | |
22 | 22 | class Config extends ConfigurationOption |
23 | 23 | { |
24 | - /** |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $host = '127.0.0.1'; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - protected $port = 5984; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $username = ''; |
|
38 | - /** |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - protected $password = ''; |
|
42 | - /** |
|
43 | - * @var bool |
|
44 | - */ |
|
45 | - protected $ssl = false; |
|
46 | - /** |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - protected $timeout = 10; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $database = Driver::COUCHDB_DEFAULT_DB_NAME; |
|
55 | - |
|
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function getDatabase(): string |
|
60 | - { |
|
61 | - return $this->database; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * @param string $database |
|
66 | - * @return Config |
|
67 | - */ |
|
68 | - public function setDatabase(string $database): Config |
|
69 | - { |
|
70 | - /** @see https://docs.couchdb.org/en/latest/api/database/common.html#put--db */ |
|
71 | - if(\preg_match('#^[a-z][a-z0-9_\-+\$()/]+$#', $database)){ |
|
72 | - $this->database = $database; |
|
73 | - return $this; |
|
74 | - } |
|
75 | - |
|
76 | - throw new PhpfastcacheInvalidArgumentException(sprintf( |
|
77 | - "Error: illegal_database_name Name: '%s'. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed. Must begin with a letter.", |
|
78 | - $database |
|
79 | - )); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function getHost(): string |
|
86 | - { |
|
87 | - return $this->host; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $host |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - public function setHost(string $host): self |
|
95 | - { |
|
96 | - $this->host = $host; |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return int |
|
102 | - */ |
|
103 | - public function getPort(): int |
|
104 | - { |
|
105 | - return $this->port; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param int $port |
|
110 | - * @return self |
|
111 | - */ |
|
112 | - public function setPort(int $port): self |
|
113 | - { |
|
114 | - $this->port = $port; |
|
115 | - return $this; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function getUsername(): string |
|
122 | - { |
|
123 | - return $this->username; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $username |
|
128 | - * @return self |
|
129 | - */ |
|
130 | - public function setUsername(string $username): self |
|
131 | - { |
|
132 | - $this->username = $username; |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function getPassword(): string |
|
140 | - { |
|
141 | - return $this->password; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param string $password |
|
146 | - * @return self |
|
147 | - */ |
|
148 | - public function setPassword(string $password): self |
|
149 | - { |
|
150 | - $this->password = $password; |
|
151 | - return $this; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * @return bool |
|
156 | - */ |
|
157 | - public function isSsl(): bool |
|
158 | - { |
|
159 | - return $this->ssl; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @param bool $ssl |
|
164 | - * @return self |
|
165 | - */ |
|
166 | - public function setSsl(bool $ssl): self |
|
167 | - { |
|
168 | - $this->ssl = $ssl; |
|
169 | - return $this; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @return int |
|
174 | - */ |
|
175 | - public function getTimeout(): int |
|
176 | - { |
|
177 | - return $this->timeout; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param int $timeout |
|
182 | - * @return self |
|
183 | - */ |
|
184 | - public function setTimeout(int $timeout): self |
|
185 | - { |
|
186 | - $this->timeout = $timeout; |
|
187 | - return $this; |
|
188 | - } |
|
24 | + /** |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $host = '127.0.0.1'; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + protected $port = 5984; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $username = ''; |
|
38 | + /** |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + protected $password = ''; |
|
42 | + /** |
|
43 | + * @var bool |
|
44 | + */ |
|
45 | + protected $ssl = false; |
|
46 | + /** |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + protected $timeout = 10; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $database = Driver::COUCHDB_DEFAULT_DB_NAME; |
|
55 | + |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function getDatabase(): string |
|
60 | + { |
|
61 | + return $this->database; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * @param string $database |
|
66 | + * @return Config |
|
67 | + */ |
|
68 | + public function setDatabase(string $database): Config |
|
69 | + { |
|
70 | + /** @see https://docs.couchdb.org/en/latest/api/database/common.html#put--db */ |
|
71 | + if(\preg_match('#^[a-z][a-z0-9_\-+\$()/]+$#', $database)){ |
|
72 | + $this->database = $database; |
|
73 | + return $this; |
|
74 | + } |
|
75 | + |
|
76 | + throw new PhpfastcacheInvalidArgumentException(sprintf( |
|
77 | + "Error: illegal_database_name Name: '%s'. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed. Must begin with a letter.", |
|
78 | + $database |
|
79 | + )); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function getHost(): string |
|
86 | + { |
|
87 | + return $this->host; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $host |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + public function setHost(string $host): self |
|
95 | + { |
|
96 | + $this->host = $host; |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return int |
|
102 | + */ |
|
103 | + public function getPort(): int |
|
104 | + { |
|
105 | + return $this->port; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param int $port |
|
110 | + * @return self |
|
111 | + */ |
|
112 | + public function setPort(int $port): self |
|
113 | + { |
|
114 | + $this->port = $port; |
|
115 | + return $this; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function getUsername(): string |
|
122 | + { |
|
123 | + return $this->username; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $username |
|
128 | + * @return self |
|
129 | + */ |
|
130 | + public function setUsername(string $username): self |
|
131 | + { |
|
132 | + $this->username = $username; |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function getPassword(): string |
|
140 | + { |
|
141 | + return $this->password; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param string $password |
|
146 | + * @return self |
|
147 | + */ |
|
148 | + public function setPassword(string $password): self |
|
149 | + { |
|
150 | + $this->password = $password; |
|
151 | + return $this; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * @return bool |
|
156 | + */ |
|
157 | + public function isSsl(): bool |
|
158 | + { |
|
159 | + return $this->ssl; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @param bool $ssl |
|
164 | + * @return self |
|
165 | + */ |
|
166 | + public function setSsl(bool $ssl): self |
|
167 | + { |
|
168 | + $this->ssl = $ssl; |
|
169 | + return $this; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @return int |
|
174 | + */ |
|
175 | + public function getTimeout(): int |
|
176 | + { |
|
177 | + return $this->timeout; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param int $timeout |
|
182 | + * @return self |
|
183 | + */ |
|
184 | + public function setTimeout(int $timeout): self |
|
185 | + { |
|
186 | + $this->timeout = $timeout; |
|
187 | + return $this; |
|
188 | + } |
|
189 | 189 | } |
@@ -122,9 +122,9 @@ discard block |
||
122 | 122 | */ |
123 | 123 | protected function createDatabase() |
124 | 124 | { |
125 | - try{ |
|
125 | + try { |
|
126 | 126 | $this->instance->getDatabaseInfo($this->getDatabaseName()); |
127 | - } catch(HTTPException $e){ |
|
127 | + } catch (HTTPException $e) { |
|
128 | 128 | $this->instance->createDatabase($this->getDatabaseName()); |
129 | 129 | } |
130 | 130 | } |
@@ -279,14 +279,14 @@ discard block |
||
279 | 279 | new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone']) |
280 | 280 | ); |
281 | 281 | |
282 | - if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])){ |
|
282 | + if (isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])) { |
|
283 | 283 | $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime( |
284 | 284 | $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'], |
285 | 285 | new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone']) |
286 | 286 | ); |
287 | 287 | } |
288 | 288 | |
289 | - if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])){ |
|
289 | + if (isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])) { |
|
290 | 290 | $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime( |
291 | 291 | $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'], |
292 | 292 | new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone']) |
@@ -33,266 +33,266 @@ |
||
33 | 33 | */ |
34 | 34 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
35 | 35 | { |
36 | - public const COUCHDB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
37 | - |
|
38 | - use DriverBaseTrait; |
|
39 | - |
|
40 | - /** |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - public function driverCheck(): bool |
|
44 | - { |
|
45 | - return class_exists(CouchDBClient::class); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function getHelp(): string |
|
52 | - { |
|
53 | - return <<<HELP |
|
36 | + public const COUCHDB_DEFAULT_DB_NAME = 'phpfastcache'; // Public because used in config |
|
37 | + |
|
38 | + use DriverBaseTrait; |
|
39 | + |
|
40 | + /** |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + public function driverCheck(): bool |
|
44 | + { |
|
45 | + return class_exists(CouchDBClient::class); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function getHelp(): string |
|
52 | + { |
|
53 | + return <<<HELP |
|
54 | 54 | <p> |
55 | 55 | To install the Couchdb HTTP client library via Composer: |
56 | 56 | <code>composer require "doctrine/couchdb" "@dev"</code> |
57 | 57 | </p> |
58 | 58 | HELP; |
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @return DriverStatistic |
|
63 | - */ |
|
64 | - public function getStats(): DriverStatistic |
|
65 | - { |
|
66 | - $info = $this->instance->getDatabaseInfo(); |
|
67 | - |
|
68 | - return (new DriverStatistic()) |
|
69 | - ->setSize($info['sizes']['active'] ?? 0) |
|
70 | - ->setRawData($info) |
|
71 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
72 | - ->setInfo('Couchdb version ' . $this->instance->getVersion() . "\n For more information see RawData."); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @return bool |
|
77 | - * @throws PhpfastcacheLogicException |
|
78 | - */ |
|
79 | - protected function driverConnect(): bool |
|
80 | - { |
|
81 | - if ($this->instance instanceof CouchDBClient) { |
|
82 | - throw new PhpfastcacheLogicException('Already connected to Couchdb server'); |
|
83 | - } |
|
84 | - |
|
85 | - $clientConfig = $this->getConfig(); |
|
86 | - |
|
87 | - $url = ($clientConfig->isSsl() ? 'https://' : 'http://'); |
|
88 | - if ($clientConfig->getUsername()) { |
|
89 | - $url .= $clientConfig->getUsername(); |
|
90 | - if ($clientConfig->getPassword()) { |
|
91 | - $url .= ":{$clientConfig->getPassword()}"; |
|
92 | - } |
|
93 | - $url .= '@'; |
|
94 | - } |
|
95 | - $url .= $clientConfig->getHost(); |
|
96 | - $url .= ":{$clientConfig->getPort()}"; |
|
97 | - $url .= '/' . \urlencode($this->getDatabaseName()); |
|
98 | - |
|
99 | - $this->instance = CouchDBClient::create( |
|
100 | - [ |
|
101 | - 'dbname' => $this->getDatabaseName(), |
|
102 | - 'url' => $url, |
|
103 | - 'timeout' => $clientConfig->getTimeout(), |
|
104 | - ] |
|
105 | - ); |
|
106 | - |
|
107 | - $this->createDatabase(); |
|
108 | - |
|
109 | - return true; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - protected function getDatabaseName(): string |
|
116 | - { |
|
117 | - return $this->getConfig()->getDatabase() ?: self::COUCHDB_DEFAULT_DB_NAME; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - protected function createDatabase() |
|
124 | - { |
|
125 | - try{ |
|
126 | - $this->instance->getDatabaseInfo($this->getDatabaseName()); |
|
127 | - } catch(HTTPException $e){ |
|
128 | - $this->instance->createDatabase($this->getDatabaseName()); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - protected function getCouchDbItemKey(CacheItemInterface $item) |
|
133 | - { |
|
134 | - return 'pfc_' . $item->getEncodedKey(); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param CacheItemInterface $item |
|
139 | - * @return null|array |
|
140 | - * @throws PhpfastcacheDriverException |
|
141 | - */ |
|
142 | - protected function driverRead(CacheItemInterface $item) |
|
143 | - { |
|
144 | - try { |
|
145 | - $response = $this->instance->findDocument($this->getCouchDbItemKey($item)); |
|
146 | - } catch (CouchDBException $e) { |
|
147 | - throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e); |
|
148 | - } |
|
149 | - |
|
150 | - if ($response->status === 404 || empty($response->body[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX])) { |
|
151 | - return null; |
|
152 | - } |
|
153 | - |
|
154 | - if ($response->status === 200) { |
|
155 | - return $this->decode($response->body); |
|
156 | - } |
|
157 | - |
|
158 | - throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @param CacheItemInterface $item |
|
163 | - * @return bool |
|
164 | - * @throws PhpfastcacheDriverException |
|
165 | - * @throws PhpfastcacheInvalidArgumentException |
|
166 | - */ |
|
167 | - protected function driverWrite(CacheItemInterface $item): bool |
|
168 | - { |
|
169 | - /** |
|
170 | - * Check for Cross-Driver type confusion |
|
171 | - */ |
|
172 | - if ($item instanceof Item) { |
|
173 | - try { |
|
174 | - $this->instance->putDocument( |
|
175 | - $this->encodeDocument($this->driverPreWrap($item)), |
|
176 | - $this->getCouchDbItemKey($item), |
|
177 | - $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)) |
|
178 | - ); |
|
179 | - } catch (CouchDBException $e) { |
|
180 | - throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e); |
|
181 | - } |
|
182 | - return true; |
|
183 | - } |
|
184 | - |
|
185 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return string|null |
|
190 | - */ |
|
191 | - protected function getLatestDocumentRevision($docId) |
|
192 | - { |
|
193 | - $path = '/' . \urlencode($this->getDatabaseName()) . '/' . urlencode($docId); |
|
194 | - |
|
195 | - $response = $this->instance->getHttpClient()->request( |
|
196 | - 'HEAD', |
|
197 | - $path, |
|
198 | - null, |
|
199 | - false |
|
200 | - ); |
|
201 | - if (!empty($response->headers['etag'])) { |
|
202 | - return trim($response->headers['etag'], " '\"\t\n\r\0\x0B"); |
|
203 | - } |
|
204 | - |
|
205 | - return null; |
|
206 | - } |
|
207 | - |
|
208 | - /******************** |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @return DriverStatistic |
|
63 | + */ |
|
64 | + public function getStats(): DriverStatistic |
|
65 | + { |
|
66 | + $info = $this->instance->getDatabaseInfo(); |
|
67 | + |
|
68 | + return (new DriverStatistic()) |
|
69 | + ->setSize($info['sizes']['active'] ?? 0) |
|
70 | + ->setRawData($info) |
|
71 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
72 | + ->setInfo('Couchdb version ' . $this->instance->getVersion() . "\n For more information see RawData."); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @return bool |
|
77 | + * @throws PhpfastcacheLogicException |
|
78 | + */ |
|
79 | + protected function driverConnect(): bool |
|
80 | + { |
|
81 | + if ($this->instance instanceof CouchDBClient) { |
|
82 | + throw new PhpfastcacheLogicException('Already connected to Couchdb server'); |
|
83 | + } |
|
84 | + |
|
85 | + $clientConfig = $this->getConfig(); |
|
86 | + |
|
87 | + $url = ($clientConfig->isSsl() ? 'https://' : 'http://'); |
|
88 | + if ($clientConfig->getUsername()) { |
|
89 | + $url .= $clientConfig->getUsername(); |
|
90 | + if ($clientConfig->getPassword()) { |
|
91 | + $url .= ":{$clientConfig->getPassword()}"; |
|
92 | + } |
|
93 | + $url .= '@'; |
|
94 | + } |
|
95 | + $url .= $clientConfig->getHost(); |
|
96 | + $url .= ":{$clientConfig->getPort()}"; |
|
97 | + $url .= '/' . \urlencode($this->getDatabaseName()); |
|
98 | + |
|
99 | + $this->instance = CouchDBClient::create( |
|
100 | + [ |
|
101 | + 'dbname' => $this->getDatabaseName(), |
|
102 | + 'url' => $url, |
|
103 | + 'timeout' => $clientConfig->getTimeout(), |
|
104 | + ] |
|
105 | + ); |
|
106 | + |
|
107 | + $this->createDatabase(); |
|
108 | + |
|
109 | + return true; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + protected function getDatabaseName(): string |
|
116 | + { |
|
117 | + return $this->getConfig()->getDatabase() ?: self::COUCHDB_DEFAULT_DB_NAME; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + protected function createDatabase() |
|
124 | + { |
|
125 | + try{ |
|
126 | + $this->instance->getDatabaseInfo($this->getDatabaseName()); |
|
127 | + } catch(HTTPException $e){ |
|
128 | + $this->instance->createDatabase($this->getDatabaseName()); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + protected function getCouchDbItemKey(CacheItemInterface $item) |
|
133 | + { |
|
134 | + return 'pfc_' . $item->getEncodedKey(); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param CacheItemInterface $item |
|
139 | + * @return null|array |
|
140 | + * @throws PhpfastcacheDriverException |
|
141 | + */ |
|
142 | + protected function driverRead(CacheItemInterface $item) |
|
143 | + { |
|
144 | + try { |
|
145 | + $response = $this->instance->findDocument($this->getCouchDbItemKey($item)); |
|
146 | + } catch (CouchDBException $e) { |
|
147 | + throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e); |
|
148 | + } |
|
149 | + |
|
150 | + if ($response->status === 404 || empty($response->body[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX])) { |
|
151 | + return null; |
|
152 | + } |
|
153 | + |
|
154 | + if ($response->status === 200) { |
|
155 | + return $this->decode($response->body); |
|
156 | + } |
|
157 | + |
|
158 | + throw new PhpfastcacheDriverException('Got unexpected HTTP status: ' . $response->status); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @param CacheItemInterface $item |
|
163 | + * @return bool |
|
164 | + * @throws PhpfastcacheDriverException |
|
165 | + * @throws PhpfastcacheInvalidArgumentException |
|
166 | + */ |
|
167 | + protected function driverWrite(CacheItemInterface $item): bool |
|
168 | + { |
|
169 | + /** |
|
170 | + * Check for Cross-Driver type confusion |
|
171 | + */ |
|
172 | + if ($item instanceof Item) { |
|
173 | + try { |
|
174 | + $this->instance->putDocument( |
|
175 | + $this->encodeDocument($this->driverPreWrap($item)), |
|
176 | + $this->getCouchDbItemKey($item), |
|
177 | + $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)) |
|
178 | + ); |
|
179 | + } catch (CouchDBException $e) { |
|
180 | + throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e); |
|
181 | + } |
|
182 | + return true; |
|
183 | + } |
|
184 | + |
|
185 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return string|null |
|
190 | + */ |
|
191 | + protected function getLatestDocumentRevision($docId) |
|
192 | + { |
|
193 | + $path = '/' . \urlencode($this->getDatabaseName()) . '/' . urlencode($docId); |
|
194 | + |
|
195 | + $response = $this->instance->getHttpClient()->request( |
|
196 | + 'HEAD', |
|
197 | + $path, |
|
198 | + null, |
|
199 | + false |
|
200 | + ); |
|
201 | + if (!empty($response->headers['etag'])) { |
|
202 | + return trim($response->headers['etag'], " '\"\t\n\r\0\x0B"); |
|
203 | + } |
|
204 | + |
|
205 | + return null; |
|
206 | + } |
|
207 | + |
|
208 | + /******************** |
|
209 | 209 | * |
210 | 210 | * PSR-6 Extended Methods |
211 | 211 | * |
212 | 212 | *******************/ |
213 | 213 | |
214 | - /** |
|
215 | - * @param CacheItemInterface $item |
|
216 | - * @return bool |
|
217 | - * @throws PhpfastcacheDriverException |
|
218 | - * @throws PhpfastcacheInvalidArgumentException |
|
219 | - */ |
|
220 | - protected function driverDelete(CacheItemInterface $item): bool |
|
221 | - { |
|
222 | - /** |
|
223 | - * Check for Cross-Driver type confusion |
|
224 | - */ |
|
225 | - if ($item instanceof Item) { |
|
226 | - try { |
|
227 | - $this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item))); |
|
228 | - } catch (CouchDBException $e) { |
|
229 | - throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e); |
|
230 | - } |
|
231 | - return true; |
|
232 | - } |
|
233 | - |
|
234 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * @return bool |
|
239 | - * @throws PhpfastcacheDriverException |
|
240 | - */ |
|
241 | - protected function driverClear(): bool |
|
242 | - { |
|
243 | - try { |
|
244 | - $this->instance->deleteDatabase($this->getDatabaseName()); |
|
245 | - $this->createDatabase(); |
|
246 | - } catch (CouchDBException $e) { |
|
247 | - throw new PhpfastcacheDriverException('Got error while trying to delete and recreate the database: ' . $e->getMessage(), 0, $e); |
|
248 | - } |
|
249 | - |
|
250 | - return true; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * @param array $data |
|
255 | - * @return array |
|
256 | - */ |
|
257 | - protected function encodeDocument(array $data): array |
|
258 | - { |
|
259 | - $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
260 | - |
|
261 | - return $data; |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Specific document decoder for Couchdb |
|
266 | - * since we dont store encoded version |
|
267 | - * for performance purposes |
|
268 | - * |
|
269 | - * @param $value |
|
270 | - * @return mixed |
|
271 | - * @throws \Exception |
|
272 | - */ |
|
273 | - protected function decode($value) |
|
274 | - { |
|
275 | - $value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => true]); |
|
276 | - |
|
277 | - $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime( |
|
278 | - $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['date'], |
|
279 | - new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone']) |
|
280 | - ); |
|
281 | - |
|
282 | - if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])){ |
|
283 | - $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime( |
|
284 | - $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'], |
|
285 | - new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone']) |
|
286 | - ); |
|
287 | - } |
|
288 | - |
|
289 | - if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])){ |
|
290 | - $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime( |
|
291 | - $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'], |
|
292 | - new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone']) |
|
293 | - ); |
|
294 | - } |
|
295 | - |
|
296 | - return $value; |
|
297 | - } |
|
214 | + /** |
|
215 | + * @param CacheItemInterface $item |
|
216 | + * @return bool |
|
217 | + * @throws PhpfastcacheDriverException |
|
218 | + * @throws PhpfastcacheInvalidArgumentException |
|
219 | + */ |
|
220 | + protected function driverDelete(CacheItemInterface $item): bool |
|
221 | + { |
|
222 | + /** |
|
223 | + * Check for Cross-Driver type confusion |
|
224 | + */ |
|
225 | + if ($item instanceof Item) { |
|
226 | + try { |
|
227 | + $this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item))); |
|
228 | + } catch (CouchDBException $e) { |
|
229 | + throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e); |
|
230 | + } |
|
231 | + return true; |
|
232 | + } |
|
233 | + |
|
234 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * @return bool |
|
239 | + * @throws PhpfastcacheDriverException |
|
240 | + */ |
|
241 | + protected function driverClear(): bool |
|
242 | + { |
|
243 | + try { |
|
244 | + $this->instance->deleteDatabase($this->getDatabaseName()); |
|
245 | + $this->createDatabase(); |
|
246 | + } catch (CouchDBException $e) { |
|
247 | + throw new PhpfastcacheDriverException('Got error while trying to delete and recreate the database: ' . $e->getMessage(), 0, $e); |
|
248 | + } |
|
249 | + |
|
250 | + return true; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * @param array $data |
|
255 | + * @return array |
|
256 | + */ |
|
257 | + protected function encodeDocument(array $data): array |
|
258 | + { |
|
259 | + $data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = $this->encode($data[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]); |
|
260 | + |
|
261 | + return $data; |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Specific document decoder for Couchdb |
|
266 | + * since we dont store encoded version |
|
267 | + * for performance purposes |
|
268 | + * |
|
269 | + * @param $value |
|
270 | + * @return mixed |
|
271 | + * @throws \Exception |
|
272 | + */ |
|
273 | + protected function decode($value) |
|
274 | + { |
|
275 | + $value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => true]); |
|
276 | + |
|
277 | + $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime( |
|
278 | + $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['date'], |
|
279 | + new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]['timezone']) |
|
280 | + ); |
|
281 | + |
|
282 | + if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX])){ |
|
283 | + $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = new \DateTime( |
|
284 | + $value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['date'], |
|
285 | + new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]['timezone']) |
|
286 | + ); |
|
287 | + } |
|
288 | + |
|
289 | + if(isset($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX])){ |
|
290 | + $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new \DateTime( |
|
291 | + $value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['date'], |
|
292 | + new \DateTimeZone($value[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]['timezone']) |
|
293 | + ); |
|
294 | + } |
|
295 | + |
|
296 | + return $value; |
|
297 | + } |
|
298 | 298 | } |
@@ -120,17 +120,16 @@ discard block |
||
120 | 120 | /** |
121 | 121 | * @return void |
122 | 122 | */ |
123 | - protected function createDatabase() |
|
124 | - { |
|
123 | + protected function createDatabase() { |
|
125 | 124 | try{ |
126 | 125 | $this->instance->getDatabaseInfo($this->getDatabaseName()); |
127 | - } catch(HTTPException $e){ |
|
126 | + } |
|
127 | + catch(HTTPException $e){ |
|
128 | 128 | $this->instance->createDatabase($this->getDatabaseName()); |
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | - protected function getCouchDbItemKey(CacheItemInterface $item) |
|
133 | - { |
|
132 | + protected function getCouchDbItemKey(CacheItemInterface $item) { |
|
134 | 133 | return 'pfc_' . $item->getEncodedKey(); |
135 | 134 | } |
136 | 135 | |
@@ -139,11 +138,11 @@ discard block |
||
139 | 138 | * @return null|array |
140 | 139 | * @throws PhpfastcacheDriverException |
141 | 140 | */ |
142 | - protected function driverRead(CacheItemInterface $item) |
|
143 | - { |
|
141 | + protected function driverRead(CacheItemInterface $item) { |
|
144 | 142 | try { |
145 | 143 | $response = $this->instance->findDocument($this->getCouchDbItemKey($item)); |
146 | - } catch (CouchDBException $e) { |
|
144 | + } |
|
145 | + catch (CouchDBException $e) { |
|
147 | 146 | throw new PhpfastcacheDriverException('Got error while trying to get a document: ' . $e->getMessage(), 0, $e); |
148 | 147 | } |
149 | 148 | |
@@ -176,7 +175,8 @@ discard block |
||
176 | 175 | $this->getCouchDbItemKey($item), |
177 | 176 | $this->getLatestDocumentRevision($this->getCouchDbItemKey($item)) |
178 | 177 | ); |
179 | - } catch (CouchDBException $e) { |
|
178 | + } |
|
179 | + catch (CouchDBException $e) { |
|
180 | 180 | throw new PhpfastcacheDriverException('Got error while trying to upsert a document: ' . $e->getMessage(), 0, $e); |
181 | 181 | } |
182 | 182 | return true; |
@@ -188,8 +188,7 @@ discard block |
||
188 | 188 | /** |
189 | 189 | * @return string|null |
190 | 190 | */ |
191 | - protected function getLatestDocumentRevision($docId) |
|
192 | - { |
|
191 | + protected function getLatestDocumentRevision($docId) { |
|
193 | 192 | $path = '/' . \urlencode($this->getDatabaseName()) . '/' . urlencode($docId); |
194 | 193 | |
195 | 194 | $response = $this->instance->getHttpClient()->request( |
@@ -225,7 +224,8 @@ discard block |
||
225 | 224 | if ($item instanceof Item) { |
226 | 225 | try { |
227 | 226 | $this->instance->deleteDocument($this->getCouchDbItemKey($item), $this->getLatestDocumentRevision($this->getCouchDbItemKey($item))); |
228 | - } catch (CouchDBException $e) { |
|
227 | + } |
|
228 | + catch (CouchDBException $e) { |
|
229 | 229 | throw new PhpfastcacheDriverException('Got error while trying to delete a document: ' . $e->getMessage(), 0, $e); |
230 | 230 | } |
231 | 231 | return true; |
@@ -243,7 +243,8 @@ discard block |
||
243 | 243 | try { |
244 | 244 | $this->instance->deleteDatabase($this->getDatabaseName()); |
245 | 245 | $this->createDatabase(); |
246 | - } catch (CouchDBException $e) { |
|
246 | + } |
|
247 | + catch (CouchDBException $e) { |
|
247 | 248 | throw new PhpfastcacheDriverException('Got error while trying to delete and recreate the database: ' . $e->getMessage(), 0, $e); |
248 | 249 | } |
249 | 250 | |
@@ -270,8 +271,7 @@ discard block |
||
270 | 271 | * @return mixed |
271 | 272 | * @throws \Exception |
272 | 273 | */ |
273 | - protected function decode($value) |
|
274 | - { |
|
274 | + protected function decode($value) { |
|
275 | 275 | $value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX] = \unserialize($value[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX], ['allowed_classes' => true]); |
276 | 276 | |
277 | 277 | $value[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] = new \DateTime( |
@@ -76,9 +76,9 @@ |
||
76 | 76 | { |
77 | 77 | $file_path = $this->getFilePath($item->getKey(), true); |
78 | 78 | |
79 | - try{ |
|
79 | + try { |
|
80 | 80 | $content = $this->readFile($file_path); |
81 | - }catch (PhpfastcacheIOException $e){ |
|
81 | + } catch (PhpfastcacheIOException $e) { |
|
82 | 82 | return null; |
83 | 83 | } |
84 | 84 |
@@ -37,114 +37,114 @@ |
||
37 | 37 | */ |
38 | 38 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
39 | 39 | { |
40 | - use IOHelperTrait; |
|
41 | - use DriverBaseTrait { |
|
42 | - DriverBaseTrait::__construct as private __parentConstruct; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Driver constructor. |
|
47 | - * @param Config $config |
|
48 | - * @param string $instanceId |
|
49 | - */ |
|
50 | - public function __construct(Config $config, string $instanceId) |
|
51 | - { |
|
52 | - $this->__parentConstruct($config, $instanceId); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public function driverCheck(): bool |
|
59 | - { |
|
60 | - return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - protected function driverConnect(): bool |
|
67 | - { |
|
68 | - return true; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param CacheItemInterface $item |
|
73 | - * @return null|array |
|
74 | - */ |
|
75 | - protected function driverRead(CacheItemInterface $item) |
|
76 | - { |
|
77 | - $file_path = $this->getFilePath($item->getKey(), true); |
|
78 | - |
|
79 | - try{ |
|
80 | - $content = $this->readFile($file_path); |
|
81 | - }catch (PhpfastcacheIOException $e){ |
|
82 | - return null; |
|
83 | - } |
|
84 | - |
|
85 | - return $this->decode($content); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param CacheItemInterface $item |
|
90 | - * @return bool |
|
91 | - * @throws PhpfastcacheInvalidArgumentException |
|
92 | - */ |
|
93 | - protected function driverWrite(CacheItemInterface $item): bool |
|
94 | - { |
|
95 | - /** |
|
96 | - * Check for Cross-Driver type confusion |
|
97 | - */ |
|
98 | - if ($item instanceof Item) { |
|
99 | - $file_path = $this->getFilePath($item->getKey()); |
|
100 | - $data = $this->encode($this->driverPreWrap($item)); |
|
101 | - |
|
102 | - /** |
|
103 | - * Force write |
|
104 | - */ |
|
105 | - try { |
|
106 | - return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation()); |
|
107 | - } catch (Exception $e) { |
|
108 | - return false; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @param CacheItemInterface $item |
|
117 | - * @return bool |
|
118 | - * @throws PhpfastcacheInvalidArgumentException |
|
119 | - */ |
|
120 | - protected function driverDelete(CacheItemInterface $item): bool |
|
121 | - { |
|
122 | - /** |
|
123 | - * Check for Cross-Driver type confusion |
|
124 | - */ |
|
125 | - if ($item instanceof Item) { |
|
126 | - $file_path = $this->getFilePath($item->getKey(), true); |
|
127 | - if (\file_exists($file_path) && @\unlink($file_path)) { |
|
128 | - \clearstatcache(true, $file_path); |
|
129 | - $dir = \dirname($file_path); |
|
130 | - if (!(new FilesystemIterator($dir))->valid()) { |
|
131 | - \rmdir($dir); |
|
132 | - } |
|
133 | - return true; |
|
134 | - } |
|
135 | - |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return bool |
|
144 | - * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
|
145 | - */ |
|
146 | - protected function driverClear(): bool |
|
147 | - { |
|
148 | - return Directory::rrmdir($this->getPath(true)); |
|
149 | - } |
|
40 | + use IOHelperTrait; |
|
41 | + use DriverBaseTrait { |
|
42 | + DriverBaseTrait::__construct as private __parentConstruct; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Driver constructor. |
|
47 | + * @param Config $config |
|
48 | + * @param string $instanceId |
|
49 | + */ |
|
50 | + public function __construct(Config $config, string $instanceId) |
|
51 | + { |
|
52 | + $this->__parentConstruct($config, $instanceId); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public function driverCheck(): bool |
|
59 | + { |
|
60 | + return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + protected function driverConnect(): bool |
|
67 | + { |
|
68 | + return true; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param CacheItemInterface $item |
|
73 | + * @return null|array |
|
74 | + */ |
|
75 | + protected function driverRead(CacheItemInterface $item) |
|
76 | + { |
|
77 | + $file_path = $this->getFilePath($item->getKey(), true); |
|
78 | + |
|
79 | + try{ |
|
80 | + $content = $this->readFile($file_path); |
|
81 | + }catch (PhpfastcacheIOException $e){ |
|
82 | + return null; |
|
83 | + } |
|
84 | + |
|
85 | + return $this->decode($content); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param CacheItemInterface $item |
|
90 | + * @return bool |
|
91 | + * @throws PhpfastcacheInvalidArgumentException |
|
92 | + */ |
|
93 | + protected function driverWrite(CacheItemInterface $item): bool |
|
94 | + { |
|
95 | + /** |
|
96 | + * Check for Cross-Driver type confusion |
|
97 | + */ |
|
98 | + if ($item instanceof Item) { |
|
99 | + $file_path = $this->getFilePath($item->getKey()); |
|
100 | + $data = $this->encode($this->driverPreWrap($item)); |
|
101 | + |
|
102 | + /** |
|
103 | + * Force write |
|
104 | + */ |
|
105 | + try { |
|
106 | + return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation()); |
|
107 | + } catch (Exception $e) { |
|
108 | + return false; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @param CacheItemInterface $item |
|
117 | + * @return bool |
|
118 | + * @throws PhpfastcacheInvalidArgumentException |
|
119 | + */ |
|
120 | + protected function driverDelete(CacheItemInterface $item): bool |
|
121 | + { |
|
122 | + /** |
|
123 | + * Check for Cross-Driver type confusion |
|
124 | + */ |
|
125 | + if ($item instanceof Item) { |
|
126 | + $file_path = $this->getFilePath($item->getKey(), true); |
|
127 | + if (\file_exists($file_path) && @\unlink($file_path)) { |
|
128 | + \clearstatcache(true, $file_path); |
|
129 | + $dir = \dirname($file_path); |
|
130 | + if (!(new FilesystemIterator($dir))->valid()) { |
|
131 | + \rmdir($dir); |
|
132 | + } |
|
133 | + return true; |
|
134 | + } |
|
135 | + |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return bool |
|
144 | + * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
|
145 | + */ |
|
146 | + protected function driverClear(): bool |
|
147 | + { |
|
148 | + return Directory::rrmdir($this->getPath(true)); |
|
149 | + } |
|
150 | 150 | } |
@@ -47,8 +47,7 @@ discard block |
||
47 | 47 | * @param Config $config |
48 | 48 | * @param string $instanceId |
49 | 49 | */ |
50 | - public function __construct(Config $config, string $instanceId) |
|
51 | - { |
|
50 | + public function __construct(Config $config, string $instanceId) { |
|
52 | 51 | $this->__parentConstruct($config, $instanceId); |
53 | 52 | } |
54 | 53 | |
@@ -72,13 +71,13 @@ discard block |
||
72 | 71 | * @param CacheItemInterface $item |
73 | 72 | * @return null|array |
74 | 73 | */ |
75 | - protected function driverRead(CacheItemInterface $item) |
|
76 | - { |
|
74 | + protected function driverRead(CacheItemInterface $item) { |
|
77 | 75 | $file_path = $this->getFilePath($item->getKey(), true); |
78 | 76 | |
79 | 77 | try{ |
80 | 78 | $content = $this->readFile($file_path); |
81 | - }catch (PhpfastcacheIOException $e){ |
|
79 | + } |
|
80 | + catch (PhpfastcacheIOException $e){ |
|
82 | 81 | return null; |
83 | 82 | } |
84 | 83 | |
@@ -104,7 +103,8 @@ discard block |
||
104 | 103 | */ |
105 | 104 | try { |
106 | 105 | return $this->writefile($file_path, $data, $this->getConfig()->isSecureFileManipulation()); |
107 | - } catch (Exception $e) { |
|
106 | + } |
|
107 | + catch (Exception $e) { |
|
108 | 108 | return false; |
109 | 109 | } |
110 | 110 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * Check for Cross-Driver type confusion |
118 | 118 | */ |
119 | 119 | if ($item instanceof Item) { |
120 | - return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
120 | + return (bool) $this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | * Check for Cross-Driver type confusion |
135 | 135 | */ |
136 | 136 | if ($item instanceof Item) { |
137 | - return (bool)$this->instance->del($item->getEncodedKey()); |
|
137 | + return (bool) $this->instance->del($item->getEncodedKey()); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
@@ -151,6 +151,6 @@ discard block |
||
151 | 151 | */ |
152 | 152 | protected function driverClear(): bool |
153 | 153 | { |
154 | - return (bool)$this->instance->flushdb('kv'); |
|
154 | + return (bool) $this->instance->flushdb('kv'); |
|
155 | 155 | } |
156 | 156 | } |
@@ -33,124 +33,124 @@ |
||
33 | 33 | */ |
34 | 34 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
35 | 35 | { |
36 | - use DriverBaseTrait; |
|
37 | - |
|
38 | - /** |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function driverCheck(): bool |
|
42 | - { |
|
43 | - static $driverCheck; |
|
44 | - if ($driverCheck === null) { |
|
45 | - return ($driverCheck = class_exists('phpssdb\Core\SSDB')); |
|
46 | - } |
|
47 | - |
|
48 | - return $driverCheck; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @return DriverStatistic |
|
53 | - */ |
|
54 | - public function getStats(): DriverStatistic |
|
55 | - { |
|
56 | - $stat = new DriverStatistic(); |
|
57 | - $info = $this->instance->info(); |
|
58 | - |
|
59 | - /** |
|
60 | - * Data returned by Ssdb are very poorly formatted |
|
61 | - * using hardcoded offset of pair key-value :-( |
|
62 | - */ |
|
63 | - $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6])) |
|
64 | - ->setRawData($info) |
|
65 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
66 | - ->setSize($this->instance->dbsize()); |
|
67 | - |
|
68 | - return $stat; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return bool |
|
73 | - * @throws PhpfastcacheDriverException |
|
74 | - */ |
|
75 | - protected function driverConnect(): bool |
|
76 | - { |
|
77 | - try { |
|
78 | - $clientConfig = $this->getConfig(); |
|
79 | - |
|
80 | - $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout()); |
|
81 | - if (!empty($clientConfig->getPassword())) { |
|
82 | - $this->instance->auth($clientConfig->getPassword()); |
|
83 | - } |
|
84 | - |
|
85 | - if (!$this->instance) { |
|
86 | - return false; |
|
87 | - } |
|
88 | - |
|
89 | - return true; |
|
90 | - } catch (SSDBException $e) { |
|
91 | - throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param CacheItemInterface $item |
|
97 | - * @return null|array |
|
98 | - */ |
|
99 | - protected function driverRead(CacheItemInterface $item) |
|
100 | - { |
|
101 | - $val = $this->instance->get($item->getEncodedKey()); |
|
102 | - if ($val == false) { |
|
103 | - return null; |
|
104 | - } |
|
105 | - |
|
106 | - return $this->decode($val); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @param CacheItemInterface $item |
|
111 | - * @return mixed |
|
112 | - * @throws PhpfastcacheInvalidArgumentException |
|
113 | - */ |
|
114 | - protected function driverWrite(CacheItemInterface $item): bool |
|
115 | - { |
|
116 | - /** |
|
117 | - * Check for Cross-Driver type confusion |
|
118 | - */ |
|
119 | - if ($item instanceof Item) { |
|
120 | - return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
121 | - } |
|
122 | - |
|
123 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param CacheItemInterface $item |
|
128 | - * @return bool |
|
129 | - * @throws PhpfastcacheInvalidArgumentException |
|
130 | - */ |
|
131 | - protected function driverDelete(CacheItemInterface $item): bool |
|
132 | - { |
|
133 | - /** |
|
134 | - * Check for Cross-Driver type confusion |
|
135 | - */ |
|
136 | - if ($item instanceof Item) { |
|
137 | - return (bool)$this->instance->del($item->getEncodedKey()); |
|
138 | - } |
|
139 | - |
|
140 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
141 | - } |
|
142 | - |
|
143 | - /******************** |
|
36 | + use DriverBaseTrait; |
|
37 | + |
|
38 | + /** |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function driverCheck(): bool |
|
42 | + { |
|
43 | + static $driverCheck; |
|
44 | + if ($driverCheck === null) { |
|
45 | + return ($driverCheck = class_exists('phpssdb\Core\SSDB')); |
|
46 | + } |
|
47 | + |
|
48 | + return $driverCheck; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @return DriverStatistic |
|
53 | + */ |
|
54 | + public function getStats(): DriverStatistic |
|
55 | + { |
|
56 | + $stat = new DriverStatistic(); |
|
57 | + $info = $this->instance->info(); |
|
58 | + |
|
59 | + /** |
|
60 | + * Data returned by Ssdb are very poorly formatted |
|
61 | + * using hardcoded offset of pair key-value :-( |
|
62 | + */ |
|
63 | + $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6])) |
|
64 | + ->setRawData($info) |
|
65 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
66 | + ->setSize($this->instance->dbsize()); |
|
67 | + |
|
68 | + return $stat; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return bool |
|
73 | + * @throws PhpfastcacheDriverException |
|
74 | + */ |
|
75 | + protected function driverConnect(): bool |
|
76 | + { |
|
77 | + try { |
|
78 | + $clientConfig = $this->getConfig(); |
|
79 | + |
|
80 | + $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout()); |
|
81 | + if (!empty($clientConfig->getPassword())) { |
|
82 | + $this->instance->auth($clientConfig->getPassword()); |
|
83 | + } |
|
84 | + |
|
85 | + if (!$this->instance) { |
|
86 | + return false; |
|
87 | + } |
|
88 | + |
|
89 | + return true; |
|
90 | + } catch (SSDBException $e) { |
|
91 | + throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param CacheItemInterface $item |
|
97 | + * @return null|array |
|
98 | + */ |
|
99 | + protected function driverRead(CacheItemInterface $item) |
|
100 | + { |
|
101 | + $val = $this->instance->get($item->getEncodedKey()); |
|
102 | + if ($val == false) { |
|
103 | + return null; |
|
104 | + } |
|
105 | + |
|
106 | + return $this->decode($val); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @param CacheItemInterface $item |
|
111 | + * @return mixed |
|
112 | + * @throws PhpfastcacheInvalidArgumentException |
|
113 | + */ |
|
114 | + protected function driverWrite(CacheItemInterface $item): bool |
|
115 | + { |
|
116 | + /** |
|
117 | + * Check for Cross-Driver type confusion |
|
118 | + */ |
|
119 | + if ($item instanceof Item) { |
|
120 | + return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl()); |
|
121 | + } |
|
122 | + |
|
123 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param CacheItemInterface $item |
|
128 | + * @return bool |
|
129 | + * @throws PhpfastcacheInvalidArgumentException |
|
130 | + */ |
|
131 | + protected function driverDelete(CacheItemInterface $item): bool |
|
132 | + { |
|
133 | + /** |
|
134 | + * Check for Cross-Driver type confusion |
|
135 | + */ |
|
136 | + if ($item instanceof Item) { |
|
137 | + return (bool)$this->instance->del($item->getEncodedKey()); |
|
138 | + } |
|
139 | + |
|
140 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
141 | + } |
|
142 | + |
|
143 | + /******************** |
|
144 | 144 | * |
145 | 145 | * PSR-6 Extended Methods |
146 | 146 | * |
147 | 147 | *******************/ |
148 | 148 | |
149 | - /** |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - protected function driverClear(): bool |
|
153 | - { |
|
154 | - return (bool)$this->instance->flushdb('kv'); |
|
155 | - } |
|
149 | + /** |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + protected function driverClear(): bool |
|
153 | + { |
|
154 | + return (bool)$this->instance->flushdb('kv'); |
|
155 | + } |
|
156 | 156 | } |
@@ -87,7 +87,8 @@ discard block |
||
87 | 87 | } |
88 | 88 | |
89 | 89 | return true; |
90 | - } catch (SSDBException $e) { |
|
90 | + } |
|
91 | + catch (SSDBException $e) { |
|
91 | 92 | throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e); |
92 | 93 | } |
93 | 94 | } |
@@ -96,8 +97,7 @@ discard block |
||
96 | 97 | * @param CacheItemInterface $item |
97 | 98 | * @return null|array |
98 | 99 | */ |
99 | - protected function driverRead(CacheItemInterface $item) |
|
100 | - { |
|
100 | + protected function driverRead(CacheItemInterface $item) { |
|
101 | 101 | $val = $this->instance->get($item->getEncodedKey()); |
102 | 102 | if ($val == false) { |
103 | 103 | return null; |
@@ -125,7 +125,7 @@ |
||
125 | 125 | * Check for Cross-Driver type confusion |
126 | 126 | */ |
127 | 127 | if ($item instanceof Item) { |
128 | - return (bool)zend_disk_cache_delete($item->getKey()); |
|
128 | + return (bool) zend_disk_cache_delete($item->getKey()); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
@@ -31,111 +31,111 @@ |
||
31 | 31 | */ |
32 | 32 | class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface |
33 | 33 | { |
34 | - use DriverBaseTrait; |
|
35 | - |
|
36 | - /** |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function driverCheck(): bool |
|
40 | - { |
|
41 | - return extension_loaded('Zend Data Cache') && function_exists('zend_disk_cache_store'); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function getHelp(): string |
|
48 | - { |
|
49 | - return <<<HELP |
|
34 | + use DriverBaseTrait; |
|
35 | + |
|
36 | + /** |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function driverCheck(): bool |
|
40 | + { |
|
41 | + return extension_loaded('Zend Data Cache') && function_exists('zend_disk_cache_store'); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function getHelp(): string |
|
48 | + { |
|
49 | + return <<<HELP |
|
50 | 50 | <p> |
51 | 51 | This driver rely on Zend Server 8.5+, see: https://www.zend.com/en/products/zend_server |
52 | 52 | </p> |
53 | 53 | HELP; |
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @return DriverStatistic |
|
58 | - */ |
|
59 | - public function getStats(): DriverStatistic |
|
60 | - { |
|
61 | - $stat = new DriverStatistic(); |
|
62 | - $stat->setInfo('[ZendDisk] A void info string') |
|
63 | - ->setSize(0) |
|
64 | - ->setData(implode(', ', array_keys($this->itemInstances))) |
|
65 | - ->setRawData(false); |
|
66 | - |
|
67 | - return $stat; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return bool |
|
72 | - */ |
|
73 | - protected function driverConnect(): bool |
|
74 | - { |
|
75 | - return true; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param CacheItemInterface $item |
|
80 | - * @return null|array |
|
81 | - */ |
|
82 | - protected function driverRead(CacheItemInterface $item) |
|
83 | - { |
|
84 | - $data = zend_disk_cache_fetch($item->getKey()); |
|
85 | - if ($data === false) { |
|
86 | - return null; |
|
87 | - } |
|
88 | - |
|
89 | - return $data; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param CacheItemInterface $item |
|
94 | - * @return mixed |
|
95 | - * @throws PhpfastcacheInvalidArgumentException |
|
96 | - */ |
|
97 | - protected function driverWrite(CacheItemInterface $item): bool |
|
98 | - { |
|
99 | - /** |
|
100 | - * Check for Cross-Driver type confusion |
|
101 | - */ |
|
102 | - if ($item instanceof Item) { |
|
103 | - $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
104 | - |
|
105 | - return zend_disk_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
|
106 | - } |
|
107 | - |
|
108 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
109 | - } |
|
110 | - |
|
111 | - /******************** |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @return DriverStatistic |
|
58 | + */ |
|
59 | + public function getStats(): DriverStatistic |
|
60 | + { |
|
61 | + $stat = new DriverStatistic(); |
|
62 | + $stat->setInfo('[ZendDisk] A void info string') |
|
63 | + ->setSize(0) |
|
64 | + ->setData(implode(', ', array_keys($this->itemInstances))) |
|
65 | + ->setRawData(false); |
|
66 | + |
|
67 | + return $stat; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return bool |
|
72 | + */ |
|
73 | + protected function driverConnect(): bool |
|
74 | + { |
|
75 | + return true; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param CacheItemInterface $item |
|
80 | + * @return null|array |
|
81 | + */ |
|
82 | + protected function driverRead(CacheItemInterface $item) |
|
83 | + { |
|
84 | + $data = zend_disk_cache_fetch($item->getKey()); |
|
85 | + if ($data === false) { |
|
86 | + return null; |
|
87 | + } |
|
88 | + |
|
89 | + return $data; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param CacheItemInterface $item |
|
94 | + * @return mixed |
|
95 | + * @throws PhpfastcacheInvalidArgumentException |
|
96 | + */ |
|
97 | + protected function driverWrite(CacheItemInterface $item): bool |
|
98 | + { |
|
99 | + /** |
|
100 | + * Check for Cross-Driver type confusion |
|
101 | + */ |
|
102 | + if ($item instanceof Item) { |
|
103 | + $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
104 | + |
|
105 | + return zend_disk_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
|
106 | + } |
|
107 | + |
|
108 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
109 | + } |
|
110 | + |
|
111 | + /******************** |
|
112 | 112 | * |
113 | 113 | * PSR-6 Extended Methods |
114 | 114 | * |
115 | 115 | *******************/ |
116 | 116 | |
117 | - /** |
|
118 | - * @param CacheItemInterface $item |
|
119 | - * @return bool |
|
120 | - * @throws PhpfastcacheInvalidArgumentException |
|
121 | - */ |
|
122 | - protected function driverDelete(CacheItemInterface $item): bool |
|
123 | - { |
|
124 | - /** |
|
125 | - * Check for Cross-Driver type confusion |
|
126 | - */ |
|
127 | - if ($item instanceof Item) { |
|
128 | - return (bool)zend_disk_cache_delete($item->getKey()); |
|
129 | - } |
|
130 | - |
|
131 | - throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - protected function driverClear(): bool |
|
138 | - { |
|
139 | - return @zend_disk_cache_clear(); |
|
140 | - } |
|
117 | + /** |
|
118 | + * @param CacheItemInterface $item |
|
119 | + * @return bool |
|
120 | + * @throws PhpfastcacheInvalidArgumentException |
|
121 | + */ |
|
122 | + protected function driverDelete(CacheItemInterface $item): bool |
|
123 | + { |
|
124 | + /** |
|
125 | + * Check for Cross-Driver type confusion |
|
126 | + */ |
|
127 | + if ($item instanceof Item) { |
|
128 | + return (bool)zend_disk_cache_delete($item->getKey()); |
|
129 | + } |
|
130 | + |
|
131 | + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + protected function driverClear(): bool |
|
138 | + { |
|
139 | + return @zend_disk_cache_clear(); |
|
140 | + } |
|
141 | 141 | } |
@@ -79,8 +79,7 @@ |
||
79 | 79 | * @param CacheItemInterface $item |
80 | 80 | * @return null|array |
81 | 81 | */ |
82 | - protected function driverRead(CacheItemInterface $item) |
|
83 | - { |
|
82 | + protected function driverRead(CacheItemInterface $item) { |
|
84 | 83 | $data = zend_disk_cache_fetch($item->getKey()); |
85 | 84 | if ($data === false) { |
86 | 85 | return null; |