@@ -25,6 +25,7 @@ class TagAwareAdapter implements TagAwareAdapterInterface
2525 private $ itemsAdapter ;
2626 private $ deferred = array ();
2727 private $ createCacheItem ;
28+ private $ setCacheItemTags ;
2829 private $ getTagsByKey ;
2930 private $ tagsAdapter ;
3031
@@ -33,16 +34,29 @@ public function __construct(AdapterInterface $itemsAdapter, AdapterInterface $ta
3334 $ this ->itemsAdapter = $ itemsAdapter ;
3435 $ this ->tagsAdapter = $ tagsAdapter ?: $ itemsAdapter ;
3536 $ this ->createCacheItem = \Closure::bind (
36- function ($ key , $ value = null , CacheItem $ protoItem = null ) {
37+ function ($ key , $ value ) {
3738 $ item = new CacheItem ();
3839 $ item ->key = $ key ;
3940 $ item ->value = $ value ;
40- $ item ->isHit = false ;
4141
42- if (null !== $ protoItem ) {
43- $ item ->defaultLifetime = $ protoItem ->defaultLifetime ;
44- $ item ->innerItem = $ protoItem ->innerItem ;
45- $ item ->poolHash = $ protoItem ->poolHash ;
42+ return $ item ;
43+ },
44+ null ,
45+ CacheItem::class
46+ );
47+ $ this ->setCacheItemTags = \Closure::bind (
48+ function (CacheItem $ item , $ key , array &$ itemTags ) {
49+ if (!$ item ->isHit ) {
50+ return $ item ;
51+ }
52+ if (isset ($ itemTags [$ key ])) {
53+ foreach ($ itemTags [$ key ] as $ tag => $ version ) {
54+ $ item ->prevTags [$ tag ] = $ tag ;
55+ }
56+ unset($ itemTags [$ key ]);
57+ } else {
58+ $ item ->value = null ;
59+ $ item ->isHit = false ;
4660 }
4761
4862 return $ item ;
@@ -246,12 +260,12 @@ public function __destruct()
246260
247261 private function generateItems ($ items , array $ tagKeys )
248262 {
249- $ bufferedItems = $ itemTags = $ invalidKeys = array ();
250- $ f = $ this ->createCacheItem ;
263+ $ bufferedItems = $ itemTags = array ();
264+ $ f = $ this ->setCacheItemTags ;
251265
252266 foreach ($ items as $ key => $ item ) {
253267 if (!$ tagKeys ) {
254- yield $ key => isset ( $ invalidKeys [ self ::TAGS_PREFIX .$ key]) ? $ f ( $ key , null , $ item ) : $ item ;
268+ yield $ key => $ f ( $ item , self ::TAGS_PREFIX .$ key, $ itemTags ) ;
255269 continue ;
256270 }
257271 if (!isset ($ tagKeys [$ key ])) {
@@ -260,24 +274,23 @@ private function generateItems($items, array $tagKeys)
260274 }
261275
262276 unset($ tagKeys [$ key ]);
263- if ($ tags = $ item ->get ()) {
264- $ itemTags [$ key ] = $ tags ;
265- }
277+ $ itemTags [$ key ] = $ item ->get () ?: array ();
278+
266279 if (!$ tagKeys ) {
267280 $ tagVersions = $ this ->getTagVersions ($ itemTags );
268281
269282 foreach ($ itemTags as $ key => $ tags ) {
270283 foreach ($ tags as $ tag => $ version ) {
271284 if ($ tagVersions [$ tag ] !== $ version ) {
272- $ invalidKeys [$ key ] = true ;
285+ unset( $ itemTags [$ key ]) ;
273286 continue 2 ;
274287 }
275288 }
276289 }
277- $ itemTags = $ tagVersions = $ tagKeys = null ;
290+ $ tagVersions = $ tagKeys = null ;
278291
279292 foreach ($ bufferedItems as $ key => $ item ) {
280- yield $ key => isset ( $ invalidKeys [ self ::TAGS_PREFIX .$ key]) ? $ f ( $ key , null , $ item ) : $ item ;
293+ yield $ key => $ f ( $ item , self ::TAGS_PREFIX .$ key, $ itemTags ) ;
281294 }
282295 $ bufferedItems = null ;
283296 }
0 commit comments