@@ -26,24 +26,23 @@ class TagAwareAdapter implements TagAwareAdapterInterface
26
26
private $ deferred = array ();
27
27
private $ createCacheItem ;
28
28
private $ getTagsByKey ;
29
+ private $ invalidateTags ;
29
30
private $ tagsAdapter ;
30
31
31
32
public function __construct (AdapterInterface $ itemsAdapter , AdapterInterface $ tagsAdapter = null )
32
33
{
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 , CacheItem $ protoItem ) {
37
38
$ item = new CacheItem ();
38
39
$ item ->key = $ key ;
39
40
$ item ->value = $ value ;
40
41
$ item ->isHit = false ;
41
-
42
- if (null !== $ protoItem ) {
43
- $ item ->defaultLifetime = $ protoItem ->defaultLifetime ;
44
- $ item ->innerItem = $ protoItem ->innerItem ;
45
- $ item ->poolHash = $ protoItem ->poolHash ;
46
- }
42
+ $ item ->defaultLifetime = $ protoItem ->defaultLifetime ;
43
+ $ item ->expiry = $ protoItem ->expiry ;
44
+ $ item ->innerItem = $ protoItem ->innerItem ;
45
+ $ item ->poolHash = $ protoItem ->poolHash ;
47
46
48
47
return $ item ;
49
48
},
@@ -62,6 +61,20 @@ function ($deferred) {
62
61
null ,
63
62
CacheItem::class
64
63
);
64
+ $ this ->invalidateTags = \Closure::bind (
65
+ function (AdapterInterface $ tagsAdapter , array $ tags ) {
66
+ foreach ($ tagsAdapter ->getItems ($ tags ) as $ v ) {
67
+ $ v ->set (1 + (int ) $ v ->get ());
68
+ $ v ->defaultLifetime = 0 ;
69
+ $ v ->expiry = null ;
70
+ $ tagsAdapter ->saveDeferred ($ v );
71
+ }
72
+
73
+ return $ tagsAdapter ->commit ();
74
+ },
75
+ null ,
76
+ CacheItem::class
77
+ );
65
78
}
66
79
67
80
/**
@@ -74,13 +87,9 @@ public function invalidateTags(array $tags)
74
87
$ tags [$ k ] = $ tag .static ::TAGS_PREFIX ;
75
88
}
76
89
}
90
+ $ f = $ this ->invalidateTags ;
77
91
78
- foreach ($ this ->tagsAdapter ->getItems ($ tags ) as $ v ) {
79
- $ v ->set (1 + (int ) $ v ->get ());
80
- $ this ->tagsAdapter ->saveDeferred ($ v );
81
- }
82
-
83
- return $ this ->tagsAdapter ->commit ();
92
+ return $ f ($ this ->tagsAdapter , $ tags );
84
93
}
85
94
86
95
/**
@@ -211,22 +220,23 @@ public function commit()
211
220
$ ok = true ;
212
221
213
222
if ($ this ->deferred ) {
214
- foreach ($ this ->deferred as $ key => $ item ) {
223
+ $ items = $ this ->deferred ;
224
+ foreach ($ items as $ key => $ item ) {
215
225
if (!$ this ->itemsAdapter ->saveDeferred ($ item )) {
216
226
unset($ this ->deferred [$ key ]);
217
227
$ ok = false ;
218
228
}
219
229
}
220
230
221
231
$ f = $ this ->getTagsByKey ;
222
- $ tagsByKey = $ f ($ this -> deferred );
232
+ $ tagsByKey = $ f ($ items );
223
233
$ deletedTags = $ this ->deferred = array ();
224
234
$ tagVersions = $ this ->getTagVersions ($ tagsByKey );
225
235
$ f = $ this ->createCacheItem ;
226
236
227
237
foreach ($ tagsByKey as $ key => $ tags ) {
228
238
if ($ tags ) {
229
- $ this ->itemsAdapter ->saveDeferred ($ f (static ::TAGS_PREFIX .$ key , array_intersect_key ($ tagVersions , $ tags )));
239
+ $ this ->itemsAdapter ->saveDeferred ($ f (static ::TAGS_PREFIX .$ key , array_intersect_key ($ tagVersions , $ tags ), $ items [ $ key ] ));
230
240
} else {
231
241
$ deletedTags [] = static ::TAGS_PREFIX .$ key ;
232
242
}
0 commit comments