@@ -25,6 +25,7 @@ class TagAwareAdapter implements TagAwareAdapterInterface
25
25
private $ itemsAdapter ;
26
26
private $ deferred = array ();
27
27
private $ createCacheItem ;
28
+ private $ setCacheItemTags ;
28
29
private $ getTagsByKey ;
29
30
private $ invalidateTags ;
30
31
private $ tagsAdapter ;
@@ -38,7 +39,6 @@ function ($key, $value, CacheItem $protoItem) {
38
39
$ item = new CacheItem ();
39
40
$ item ->key = $ key ;
40
41
$ item ->value = $ value ;
41
- $ item ->isHit = false ;
42
42
$ item ->defaultLifetime = $ protoItem ->defaultLifetime ;
43
43
$ item ->expiry = $ protoItem ->expiry ;
44
44
$ item ->innerItem = $ protoItem ->innerItem ;
@@ -49,6 +49,26 @@ function ($key, $value, CacheItem $protoItem) {
49
49
null ,
50
50
CacheItem::class
51
51
);
52
+ $ this ->setCacheItemTags = \Closure::bind (
53
+ function (CacheItem $ item , $ key , array &$ itemTags ) {
54
+ if (!$ item ->isHit ) {
55
+ return $ item ;
56
+ }
57
+ if (isset ($ itemTags [$ key ])) {
58
+ foreach ($ itemTags [$ key ] as $ tag => $ version ) {
59
+ $ item ->prevTags [$ tag ] = $ tag ;
60
+ }
61
+ unset($ itemTags [$ key ]);
62
+ } else {
63
+ $ item ->value = null ;
64
+ $ item ->isHit = false ;
65
+ }
66
+
67
+ return $ item ;
68
+ },
69
+ null ,
70
+ CacheItem::class
71
+ );
52
72
$ this ->getTagsByKey = \Closure::bind (
53
73
function ($ deferred ) {
54
74
$ tagsByKey = array ();
@@ -256,12 +276,12 @@ public function __destruct()
256
276
257
277
private function generateItems ($ items , array $ tagKeys )
258
278
{
259
- $ bufferedItems = $ itemTags = $ invalidKeys = array ();
260
- $ f = $ this ->createCacheItem ;
279
+ $ bufferedItems = $ itemTags = array ();
280
+ $ f = $ this ->setCacheItemTags ;
261
281
262
282
foreach ($ items as $ key => $ item ) {
263
283
if (!$ tagKeys ) {
264
- yield $ key => isset ( $ invalidKeys [ self ::TAGS_PREFIX .$ key]) ? $ f ( $ key , null , $ item ) : $ item ;
284
+ yield $ key => $ f ( $ item , self ::TAGS_PREFIX .$ key, $ itemTags ) ;
265
285
continue ;
266
286
}
267
287
if (!isset ($ tagKeys [$ key ])) {
@@ -270,24 +290,23 @@ private function generateItems($items, array $tagKeys)
270
290
}
271
291
272
292
unset($ tagKeys [$ key ]);
273
- if ($ tags = $ item ->get ()) {
274
- $ itemTags [$ key ] = $ tags ;
275
- }
293
+ $ itemTags [$ key ] = $ item ->get () ?: array ();
294
+
276
295
if (!$ tagKeys ) {
277
296
$ tagVersions = $ this ->getTagVersions ($ itemTags );
278
297
279
298
foreach ($ itemTags as $ key => $ tags ) {
280
299
foreach ($ tags as $ tag => $ version ) {
281
300
if ($ tagVersions [$ tag ] !== $ version ) {
282
- $ invalidKeys [$ key ] = true ;
301
+ unset( $ itemTags [$ key ]) ;
283
302
continue 2 ;
284
303
}
285
304
}
286
305
}
287
- $ itemTags = $ tagVersions = $ tagKeys = null ;
306
+ $ tagVersions = $ tagKeys = null ;
288
307
289
308
foreach ($ bufferedItems as $ key => $ item ) {
290
- yield $ key => isset ( $ invalidKeys [ self ::TAGS_PREFIX .$ key]) ? $ f ( $ key , null , $ item ) : $ item ;
309
+ yield $ key => $ f ( $ item , self ::TAGS_PREFIX .$ key, $ itemTags ) ;
291
310
}
292
311
$ bufferedItems = null ;
293
312
}
0 commit comments