Total Complexity | 55 |
Total Lines | 453 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like CacheItemPoolTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CacheItemPoolTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | trait CacheItemPoolTrait |
||
40 | { |
||
41 | use DriverBaseTrait { |
||
42 | DriverBaseTrait::__construct as __driverBaseConstruct; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected static string $unsupportedKeyChars = '{}()/\@:'; |
||
49 | |||
50 | /** |
||
51 | * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
||
52 | */ |
||
53 | protected array $deferredList = []; |
||
54 | |||
55 | /** |
||
56 | * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
||
57 | */ |
||
58 | protected array $itemInstances = []; |
||
59 | |||
60 | protected DriverIO $IO; |
||
61 | |||
62 | public function __construct(ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
||
63 | { |
||
64 | $this->IO = new DriverIO(); |
||
65 | $this->__driverBaseConstruct($config, $instanceId, $em); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @throws PhpfastcacheLogicException |
||
70 | * @throws PhpfastcacheInvalidArgumentException |
||
71 | */ |
||
72 | public function setItem(CacheItemInterface $item): static |
||
73 | { |
||
74 | if (self::getItemClass() === $item::class) { |
||
75 | if (!$this->getConfig()->isUseStaticItemCaching()) { |
||
76 | throw new PhpfastcacheLogicException( |
||
77 | 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | $this->itemInstances[$item->getKey()] = $item; |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | throw new PhpfastcacheInvalidArgumentException( |
||
86 | \sprintf( |
||
87 | 'Invalid cache item class "%s" for driver "%s".', |
||
88 | get_class($item), |
||
89 | get_class($this) |
||
90 | ) |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @inheritDoc |
||
96 | * @throws PhpfastcacheCoreException |
||
97 | * @throws PhpfastcacheDriverException |
||
98 | * @throws PhpfastcacheInvalidArgumentException |
||
99 | * @throws PhpfastcacheLogicException |
||
100 | */ |
||
101 | public function getItems(array $keys = []): iterable |
||
102 | { |
||
103 | $collection = []; |
||
104 | |||
105 | foreach ($keys as $key) { |
||
106 | $collection[$key] = $this->getItem($key); |
||
107 | } |
||
108 | |||
109 | return $collection; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $key |
||
114 | * @return ExtendedCacheItemInterface |
||
115 | * @throws PhpfastcacheCoreException |
||
116 | * @throws PhpfastcacheInvalidArgumentException |
||
117 | * @throws PhpfastcacheLogicException |
||
118 | * @throws PhpfastcacheDriverException |
||
119 | * |
||
120 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
121 | * @SuppressWarnings(PHPMD.GotoStatement) |
||
122 | */ |
||
123 | public function getItem(string $key): ExtendedCacheItemInterface |
||
124 | { |
||
125 | /** |
||
126 | * Replace array_key_exists by isset |
||
127 | * due to performance issue on huge |
||
128 | * loop dispatching operations |
||
129 | */ |
||
130 | if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
||
131 | if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
||
132 | throw new PhpfastcacheInvalidArgumentException( |
||
133 | 'Unsupported key character detected: "' . $matches[1] . '". |
||
134 | Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | $cacheSlamsSpendSeconds = 0; |
||
139 | $itemClass = self::getItemClass(); |
||
140 | /** @var $item ExtendedCacheItemInterface */ |
||
141 | $item = new $itemClass($this, $key, $this->eventManager); |
||
142 | |||
143 | getItemDriverRead: |
||
144 | { |
||
145 | $driverArray = $this->driverRead($item); |
||
146 | |||
147 | if ($driverArray) { |
||
148 | if (!\is_array($driverArray)) { |
||
|
|||
149 | throw new PhpfastcacheCoreException( |
||
150 | sprintf( |
||
151 | 'The driverRead method returned an unexpected variable type: %s', |
||
152 | \gettype($driverArray) |
||
153 | ) |
||
154 | ); |
||
155 | } |
||
156 | $driverData = $this->driverUnwrapData($driverArray); |
||
157 | |||
158 | if ($this->getConfig()->isPreventCacheSlams()) { |
||
159 | while ($driverData instanceof ItemBatch) { |
||
160 | if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
||
161 | /** |
||
162 | * The timeout has been reached |
||
163 | * Consider that the batch has |
||
164 | * failed and serve an empty item |
||
165 | * to avoid get stuck with a |
||
166 | * batch item stored in driver |
||
167 | */ |
||
168 | goto getItemDriverExpired; |
||
169 | } |
||
170 | |||
171 | $this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds); |
||
172 | |||
173 | /** |
||
174 | * Wait for a second before |
||
175 | * attempting to get exit |
||
176 | * the current batch process |
||
177 | */ |
||
178 | \sleep(1); |
||
179 | $cacheSlamsSpendSeconds++; |
||
180 | |||
181 | goto getItemDriverRead; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | $item->set($driverData); |
||
186 | $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
||
187 | |||
188 | if ($this->getConfig()->isItemDetailedDate()) { |
||
189 | /** |
||
190 | * If the itemDetailedDate has been |
||
191 | * set after caching, we MUST inject |
||
192 | * a new DateTime object on the fly |
||
193 | */ |
||
194 | $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
||
195 | $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
||
196 | } |
||
197 | |||
198 | $item->setTags($this->driverUnwrapTags($driverArray)); |
||
199 | |||
200 | getItemDriverExpired: |
||
201 | if ($item->isExpired()) { |
||
202 | /** |
||
203 | * Using driverDelete() instead of delete() |
||
204 | * to avoid infinite loop caused by |
||
205 | * getItem() call in delete() method |
||
206 | * As we MUST return an item in any |
||
207 | * way, we do not de-register here |
||
208 | */ |
||
209 | $this->driverDelete($item); |
||
210 | |||
211 | /** |
||
212 | * Reset the Item |
||
213 | */ |
||
214 | $item->set(null) |
||
215 | ->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())) |
||
216 | ->setHit(false) |
||
217 | ->setTags([]); |
||
218 | if ($this->getConfig()->isItemDetailedDate()) { |
||
219 | /** |
||
220 | * If the itemDetailedDate has been |
||
221 | * set after caching, we MUST inject |
||
222 | * a new DateTime object on the fly |
||
223 | */ |
||
224 | $item->setCreationDate(new DateTime()); |
||
225 | $item->setModificationDate(new DateTime()); |
||
226 | } |
||
227 | } else { |
||
228 | $item->setHit(true); |
||
229 | } |
||
230 | } else { |
||
231 | $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
||
232 | } |
||
233 | } |
||
234 | } else { |
||
235 | $item = $this->itemInstances[$key]; |
||
236 | } |
||
237 | |||
238 | |||
239 | if ($item !== null) { |
||
240 | $this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item); |
||
241 | |||
242 | $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
||
243 | } |
||
244 | |||
245 | return $item; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param string $key |
||
250 | * @return bool |
||
251 | * @throws PhpfastcacheCoreException |
||
252 | * @throws PhpfastcacheDriverException |
||
253 | * @throws PhpfastcacheInvalidArgumentException |
||
254 | * @throws PhpfastcacheLogicException |
||
255 | */ |
||
256 | public function hasItem(string $key): bool |
||
257 | { |
||
258 | return $this->getItem($key)->isHit(); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @return bool |
||
263 | * @throws PhpfastcacheCoreException |
||
264 | * @throws PhpfastcacheDriverException |
||
265 | * @throws PhpfastcacheLogicException |
||
266 | * @throws PhpfastcacheIOException |
||
267 | */ |
||
268 | public function clear(): bool |
||
269 | { |
||
270 | $this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances); |
||
271 | |||
272 | $this->getIO()->incWriteHit(); |
||
273 | // Faster than detachAllItems() |
||
274 | $this->itemInstances = []; |
||
275 | |||
276 | return $this->driverClear(); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @inheritDoc |
||
281 | * @throws PhpfastcacheCoreException |
||
282 | * @throws PhpfastcacheDriverException |
||
283 | * @throws PhpfastcacheInvalidArgumentException |
||
284 | * @throws PhpfastcacheLogicException |
||
285 | */ |
||
286 | public function deleteItems(array $keys): bool |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param string $key |
||
301 | * @return bool |
||
302 | * @throws PhpfastcacheCoreException |
||
303 | * @throws PhpfastcacheDriverException |
||
304 | * @throws PhpfastcacheInvalidArgumentException |
||
305 | * @throws PhpfastcacheLogicException |
||
306 | */ |
||
307 | public function deleteItem(string $key): bool |
||
308 | { |
||
309 | $item = $this->getItem($key); |
||
310 | if ($item->isHit() && $this->driverDelete($item)) { |
||
311 | $item->setHit(false); |
||
312 | $this->getIO()->incWriteHit(); |
||
313 | |||
314 | $this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item); |
||
315 | |||
316 | /** |
||
317 | * De-register the item instance |
||
318 | * then collect gc cycles |
||
319 | */ |
||
320 | $this->deregisterItem($key); |
||
321 | |||
322 | /** |
||
323 | * Perform a tag cleanup to avoid memory leaks |
||
324 | */ |
||
325 | if (!\str_starts_with($key, TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
||
326 | $this->cleanItemTags($item); |
||
327 | } |
||
328 | |||
329 | return true; |
||
330 | } |
||
331 | |||
332 | return false; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param CacheItemInterface $item |
||
337 | * @return bool |
||
338 | */ |
||
339 | public function saveDeferred(CacheItemInterface $item): bool |
||
340 | { |
||
341 | if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
||
342 | $this->itemInstances[$item->getKey()] = $item; |
||
343 | } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
||
344 | throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
||
345 | } |
||
346 | |||
347 | $this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item); |
||
348 | $this->deferredList[$item->getKey()] = $item; |
||
349 | |||
350 | return true; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @return bool |
||
355 | * @throws PhpfastcacheCoreException |
||
356 | * @throws PhpfastcacheDriverException |
||
357 | * @throws PhpfastcacheInvalidArgumentException |
||
358 | * @throws PhpfastcacheLogicException |
||
359 | */ |
||
360 | public function commit(): bool |
||
361 | { |
||
362 | $this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList)); |
||
363 | |||
364 | if (\count($this->deferredList)) { |
||
365 | $return = true; |
||
366 | foreach ($this->deferredList as $key => $item) { |
||
367 | $result = $this->save($item); |
||
368 | if ($result !== true) { |
||
369 | unset($this->deferredList[$key]); |
||
370 | $return = $result; |
||
371 | } |
||
372 | } |
||
373 | |||
374 | return $return; |
||
375 | } |
||
376 | return false; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param CacheItemInterface $item |
||
381 | * @return bool |
||
382 | * @throws PhpfastcacheCoreException |
||
383 | * @throws PhpfastcacheDriverException |
||
384 | * @throws PhpfastcacheIOException |
||
385 | * @throws PhpfastcacheInvalidArgumentException |
||
386 | * @throws PhpfastcacheLogicException |
||
387 | */ |
||
388 | public function save(CacheItemInterface $item): bool |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * @return DriverIO |
||
443 | */ |
||
444 | public function getIO(): DriverIO |
||
445 | { |
||
446 | return $this->IO; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @internal This method de-register an item from $this->itemInstances |
||
451 | */ |
||
452 | protected function deregisterItem(string $item): static |
||
453 | { |
||
454 | unset($this->itemInstances[$item]); |
||
455 | |||
456 | if (\gc_enabled()) { |
||
457 | \gc_collect_cycles(); |
||
458 | } |
||
459 | |||
460 | return $this; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @throws PhpfastcacheLogicException |
||
465 | */ |
||
466 | public function attachItem(CacheItemInterface $item): static |
||
484 | } |
||
485 | |||
486 | public function isAttached(CacheItemInterface $item): bool |
||
487 | { |
||
488 | if (isset($this->itemInstances[$item->getKey()])) { |
||
489 | return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
||
490 | } |
||
491 | return false; |
||
492 | } |
||
493 | } |
||
494 |