@@ -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 $ tagsAdapter ;
30
31
@@ -33,16 +34,29 @@ public function __construct(AdapterInterface $itemsAdapter, AdapterInterface $ta
33
34
$ this ->itemsAdapter = $ itemsAdapter ;
34
35
$ this ->tagsAdapter = $ tagsAdapter ?: $ itemsAdapter ;
35
36
$ this ->createCacheItem = \Closure::bind (
36
- function ($ key , $ value = null , CacheItem $ protoItem = null ) {
37
+ function ($ key , $ value ) {
37
38
$ item = new CacheItem ();
38
39
$ item ->key = $ key ;
39
40
$ item ->value = $ value ;
40
- $ item ->isHit = false ;
41
41
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 ;
46
60
}
47
61
48
62
return $ item ;
@@ -246,12 +260,12 @@ public function __destruct()
246
260
247
261
private function generateItems ($ items , array $ tagKeys )
248
262
{
249
- $ bufferedItems = $ itemTags = $ invalidKeys = array ();
250
- $ f = $ this ->createCacheItem ;
263
+ $ bufferedItems = $ itemTags = array ();
264
+ $ f = $ this ->setCacheItemTags ;
251
265
252
266
foreach ($ items as $ key => $ item ) {
253
267
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 ) ;
255
269
continue ;
256
270
}
257
271
if (!isset ($ tagKeys [$ key ])) {
@@ -260,24 +274,23 @@ private function generateItems($items, array $tagKeys)
260
274
}
261
275
262
276
unset($ tagKeys [$ key ]);
263
- if ($ tags = $ item ->get ()) {
264
- $ itemTags [$ key ] = $ tags ;
265
- }
277
+ $ itemTags [$ key ] = $ item ->get () ?: array ();
278
+
266
279
if (!$ tagKeys ) {
267
280
$ tagVersions = $ this ->getTagVersions ($ itemTags );
268
281
269
282
foreach ($ itemTags as $ key => $ tags ) {
270
283
foreach ($ tags as $ tag => $ version ) {
271
284
if ($ tagVersions [$ tag ] !== $ version ) {
272
- $ invalidKeys [$ key ] = true ;
285
+ unset( $ itemTags [$ key ]) ;
273
286
continue 2 ;
274
287
}
275
288
}
276
289
}
277
- $ itemTags = $ tagVersions = $ tagKeys = null ;
290
+ $ tagVersions = $ tagKeys = null ;
278
291
279
292
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 ) ;
281
294
}
282
295
$ bufferedItems = null ;
283
296
}
0 commit comments