17
17
use Symfony \Component \Cache \ResettableInterface ;
18
18
use Symfony \Component \Cache \Traits \AbstractAdapterTrait ;
19
19
use Symfony \Component \Cache \Traits \ContractsTrait ;
20
+ use Symfony \Contracts \Cache \NamespacedPoolInterface ;
20
21
use Symfony \Contracts \Cache \TagAwareCacheInterface ;
21
22
22
23
/**
30
31
*
31
32
* @internal
32
33
*/
33
- abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, LoggerAwareInterface, ResettableInterface
34
+ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, AdapterInterface, NamespacedPoolInterface, LoggerAwareInterface, ResettableInterface
34
35
{
35
36
use AbstractAdapterTrait;
36
37
use ContractsTrait;
37
38
39
+ /**
40
+ * @internal
41
+ */
42
+ protected const NS_SEPARATOR = ': ' ;
43
+
38
44
private const TAGS_PREFIX = "\1tags \1" ;
39
45
40
46
protected function __construct (string $ namespace = '' , int $ defaultLifetime = 0 )
41
47
{
42
- $ this ->namespace = '' === $ namespace ? '' : CacheItem::validateKey ($ namespace ).': ' ;
48
+ if ('' !== $ namespace ) {
49
+ if (str_contains ($ namespace , static ::NS_SEPARATOR )) {
50
+ if (str_contains ($ namespace , static ::NS_SEPARATOR .static ::NS_SEPARATOR )) {
51
+ throw new InvalidArgumentException (\sprintf ('Cache namespace "%s" contains empty sub-namespace. ' , $ namespace ));
52
+ }
53
+ CacheItem::validateKey (str_replace (static ::NS_SEPARATOR , '' , $ namespace ));
54
+ } else {
55
+ CacheItem::validateKey ($ namespace );
56
+ }
57
+ $ this ->namespace = $ namespace .static ::NS_SEPARATOR ;
58
+ }
59
+ $ this ->rootNamespace = $ this ->namespace ;
60
+
43
61
$ this ->defaultLifetime = $ defaultLifetime ;
44
62
if (null !== $ this ->maxIdLength && \strlen ($ namespace ) > $ this ->maxIdLength - 24 ) {
45
63
throw new InvalidArgumentException (\sprintf ('Namespace must be %d chars max, %d given ("%s"). ' , $ this ->maxIdLength - 24 , \strlen ($ namespace ), $ namespace ));
@@ -70,7 +88,7 @@ static function ($key, $value, $isHit) {
70
88
CacheItem::class
71
89
);
72
90
self ::$ mergeByLifetime ??= \Closure::bind (
73
- static function ($ deferred , &$ expiredIds , $ getId , $ tagPrefix , $ defaultLifetime ) {
91
+ static function ($ deferred , &$ expiredIds , $ getId , $ tagPrefix , $ defaultLifetime, $ rootNamespace ) {
74
92
$ byLifetime = [];
75
93
$ now = microtime (true );
76
94
$ expiredIds = [];
@@ -102,10 +120,10 @@ static function ($deferred, &$expiredIds, $getId, $tagPrefix, $defaultLifetime)
102
120
$ value ['tag-operations ' ] = ['add ' => [], 'remove ' => []];
103
121
$ oldTags = $ item ->metadata [CacheItem::METADATA_TAGS ] ?? [];
104
122
foreach (array_diff_key ($ value ['tags ' ], $ oldTags ) as $ addedTag ) {
105
- $ value ['tag-operations ' ]['add ' ][] = $ getId ($ tagPrefix .$ addedTag );
123
+ $ value ['tag-operations ' ]['add ' ][] = $ getId ($ tagPrefix .$ addedTag, $ rootNamespace );
106
124
}
107
125
foreach (array_diff_key ($ oldTags , $ value ['tags ' ]) as $ removedTag ) {
108
- $ value ['tag-operations ' ]['remove ' ][] = $ getId ($ tagPrefix .$ removedTag );
126
+ $ value ['tag-operations ' ]['remove ' ][] = $ getId ($ tagPrefix .$ removedTag, $ rootNamespace );
109
127
}
110
128
$ value ['tags ' ] = array_keys ($ value ['tags ' ]);
111
129
@@ -168,7 +186,7 @@ protected function doDeleteYieldTags(array $ids): iterable
168
186
public function commit (): bool
169
187
{
170
188
$ ok = true ;
171
- $ byLifetime = (self ::$ mergeByLifetime )($ this ->deferred , $ expiredIds , $ this ->getId (...), self ::TAGS_PREFIX , $ this ->defaultLifetime );
189
+ $ byLifetime = (self ::$ mergeByLifetime )($ this ->deferred , $ expiredIds , $ this ->getId (...), self ::TAGS_PREFIX , $ this ->defaultLifetime , $ this -> rootNamespace );
172
190
$ retry = $ this ->deferred = [];
173
191
174
192
if ($ expiredIds ) {
@@ -195,7 +213,7 @@ public function commit(): bool
195
213
$ v = $ values [$ id ];
196
214
$ type = get_debug_type ($ v );
197
215
$ message = \sprintf ('Failed to save key "{key}" of type %s%s ' , $ type , $ e instanceof \Exception ? ': ' .$ e ->getMessage () : '. ' );
198
- CacheItem::log ($ this ->logger , $ message , ['key ' => substr ($ id , \strlen ($ this ->namespace )), 'exception ' => $ e instanceof \Exception ? $ e : null , 'cache-adapter ' => get_debug_type ($ this )]);
216
+ CacheItem::log ($ this ->logger , $ message , ['key ' => substr ($ id , \strlen ($ this ->rootNamespace )), 'exception ' => $ e instanceof \Exception ? $ e : null , 'cache-adapter ' => get_debug_type ($ this )]);
199
217
}
200
218
} else {
201
219
foreach ($ values as $ id => $ v ) {
@@ -219,7 +237,7 @@ public function commit(): bool
219
237
$ ok = false ;
220
238
$ type = get_debug_type ($ v );
221
239
$ message = \sprintf ('Failed to save key "{key}" of type %s%s ' , $ type , $ e instanceof \Exception ? ': ' .$ e ->getMessage () : '. ' );
222
- CacheItem::log ($ this ->logger , $ message , ['key ' => substr ($ id , \strlen ($ this ->namespace )), 'exception ' => $ e instanceof \Exception ? $ e : null , 'cache-adapter ' => get_debug_type ($ this )]);
240
+ CacheItem::log ($ this ->logger , $ message , ['key ' => substr ($ id , \strlen ($ this ->rootNamespace )), 'exception ' => $ e instanceof \Exception ? $ e : null , 'cache-adapter ' => get_debug_type ($ this )]);
223
241
}
224
242
}
225
243
@@ -244,7 +262,7 @@ public function deleteItems(array $keys): bool
244
262
try {
245
263
foreach ($ this ->doDeleteYieldTags (array_values ($ ids )) as $ id => $ tags ) {
246
264
foreach ($ tags as $ tag ) {
247
- $ tagData [$ this ->getId (self ::TAGS_PREFIX .$ tag )][] = $ id ;
265
+ $ tagData [$ this ->getId (self ::TAGS_PREFIX .$ tag, $ this -> rootNamespace )][] = $ id ;
248
266
}
249
267
}
250
268
} catch (\Exception ) {
@@ -283,7 +301,7 @@ public function invalidateTags(array $tags): bool
283
301
284
302
$ tagIds = [];
285
303
foreach (array_unique ($ tags ) as $ tag ) {
286
- $ tagIds [] = $ this ->getId (self ::TAGS_PREFIX .$ tag );
304
+ $ tagIds [] = $ this ->getId (self ::TAGS_PREFIX .$ tag, $ this -> rootNamespace );
287
305
}
288
306
289
307
try {
0 commit comments