@@ -128,17 +128,15 @@ public function getItem(string $key): ExtendedCacheItemInterface
128128 * loop dispatching operations
129129 */
130130 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- }
131+ $ this ->validateCacheKey ($ key );
137132
138133 $ cacheSlamsSpendSeconds = 0 ;
134+
139135 $ itemClass = self ::getItemClass ();
140136 /** @var $item ExtendedCacheItemInterface */
141137 $ item = new $ itemClass ($ this , $ key , $ this ->eventManager );
138+ // $item = new (self::getItemClass())($this, $key, $this->eventManager);
139+ // Uncomment above when this one will be fixed: https://github.com/phpmd/phpmd/issues/952
142140
143141 getItemDriverRead:
144142 {
@@ -190,35 +188,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
190188 $ item ->setTags ($ this ->driverUnwrapTags ($ driverArray ));
191189
192190 getItemDriverExpired:
193- if ($ item ->isExpired ()) {
194- /**
195- * Using driverDelete() instead of delete()
196- * to avoid infinite loop caused by
197- * getItem() call in delete() method
198- * As we MUST return an item in any
199- * way, we do not de-register here
200- */
201- $ this ->driverDelete ($ item );
202-
203- /**
204- * Reset the Item
205- */
206- $ item ->set (null )
207- ->expiresAfter ((int ) abs ($ this ->getConfig ()->getDefaultTtl ()))
208- ->setHit (false )
209- ->setTags ([]);
210- if ($ this ->getConfig ()->isItemDetailedDate ()) {
211- /**
212- * If the itemDetailedDate has been
213- * set after caching, we MUST inject
214- * a new DateTime object on the fly
215- */
216- $ item ->setCreationDate (new DateTime ());
217- $ item ->setModificationDate (new DateTime ());
218- }
219- } else {
220- $ item ->setHit (true );
221- }
191+ $ this ->handleExpiredCacheItem ($ item );
222192 } else {
223193 $ item ->expiresAfter ((int ) abs ($ this ->getConfig ()->getDefaultTtl ()));
224194 }
@@ -227,12 +197,9 @@ public function getItem(string $key): ExtendedCacheItemInterface
227197 $ item = $ this ->itemInstances [$ key ];
228198 }
229199
200+ $ this ->eventManager ->dispatch (Event::CACHE_GET_ITEM , $ this , $ item );
230201
231- if ($ item !== null ) {
232- $ this ->eventManager ->dispatch (Event::CACHE_GET_ITEM , $ this , $ item );
233-
234- $ item ->isHit () ? $ this ->getIO ()->incReadHit () : $ this ->getIO ()->incReadMiss ();
235- }
202+ $ item ->isHit () ? $ this ->getIO ()->incReadHit () : $ this ->getIO ()->incReadMiss ();
236203
237204 return $ item ;
238205 }
@@ -482,4 +449,48 @@ public function isAttached(CacheItemInterface $item): bool
482449 }
483450 return false ;
484451 }
452+
453+ protected function validateCacheKey (string $ key ): void
454+ {
455+ if (\preg_match ('~([ ' . \preg_quote (self ::$ unsupportedKeyChars , '~ ' ) . ']+)~ ' , $ key , $ matches )) {
456+ throw new PhpfastcacheInvalidArgumentException (
457+ 'Unsupported key character detected: " ' . $ matches [1 ] . '".
458+ Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers '
459+ );
460+ }
461+ }
462+
463+ protected function handleExpiredCacheItem (ExtendedCacheItemInterface $ item ): void
464+ {
465+ if ($ item ->isExpired ()) {
466+ /**
467+ * Using driverDelete() instead of delete()
468+ * to avoid infinite loop caused by
469+ * getItem() call in delete() method
470+ * As we MUST return an item in any
471+ * way, we do not de-register here
472+ */
473+ $ this ->driverDelete ($ item );
474+
475+ /**
476+ * Reset the Item
477+ */
478+ $ item ->set (null )
479+ ->expiresAfter ((int ) abs ($ this ->getConfig ()->getDefaultTtl ()))
480+ ->setHit (false )
481+ ->setTags ([]);
482+
483+ if ($ this ->getConfig ()->isItemDetailedDate ()) {
484+ /**
485+ * If the itemDetailedDate has been
486+ * set after caching, we MUST inject
487+ * a new DateTime object on the fly
488+ */
489+ $ item ->setCreationDate (new DateTime ());
490+ $ item ->setModificationDate (new DateTime ());
491+ }
492+ } else {
493+ $ item ->setHit (true );
494+ }
495+ }
485496}
0 commit comments