8000 bug #33962 [Cache] fixed TagAwareAdapter returning invalid cache (v-m-i) · symfony/symfony@1201085 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1201085

Browse files
bug #33962 [Cache] fixed TagAwareAdapter returning invalid cache (v-m-i)
This PR was merged into the 3.4 branch. Discussion ---------- [Cache] fixed TagAwareAdapter returning invalid cache | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #33953 | License | MIT | Doc PR | This PR fixes `TagAwareAdapter` returning `CacheItem` when item-tags pair is missing tag key in pool. Currently `TagAwareAdapter` returns `CacheItem` with empty tags and `isHit` set to `true`. With this PR it returns `CacheItem` with `isHit` set to `false` as we can't know if item is valid or invalid when it's missing tag entry so we treat it as cache miss. Commits ------- 946f0a1 [Cache] fixed TagAwareAdapter returning invalid cache
2 parents a8a4aad + 946f0a1 commit 1201085

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ public function hasItem($key)
156156
if (!$this->pool->hasItem($key)) {
157157
return false;
158158
}
159-
if (!$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key)->get()) {
159+
160+
if (!($itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key))->isHit()) {
161+
return false;
162+
}
163+
164+
if (!$itemTags = $itemTags->get()) {
160165
return true;
161166
}
162167

@@ -296,7 +301,10 @@ private function generateItems($items, array $tagKeys)
296301
}
297302

298303
unset($tagKeys[$key]);
299-
$itemTags[$key] = $item->get() ?: [];
304+
305+
if ($item->isHit()) {
306+
$itemTags[$key] = $item->get() ?: [];
307+
}
300308

301309
if (!$tagKeys) {
302310
$tagVersions = $this->getTagVersions($itemTags);

src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,84 @@ public function testKnownTagVersionsTtl()
194194
$this->assertTrue($pool->getItem('foo')->isHit());
195195
}
196196

197+
public function testTagEntryIsCreatedForItemWithoutTags()
198+
{
199+
$pool = $this->createCachePool();
200+
201+
$itemKey = 'foo';
202+
$item = $pool->getItem($itemKey);
203+
$pool->save($item);
204+
205+
$adapter = new FilesystemAdapter();
206+
$this->assertTrue($adapter->hasItem(TagAwareAdapter::TAGS_PREFIX.$itemKey));
207+
}
208+
209+
public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemTags()
210+
{
211+
$pool = $this->createCachePool();
212+
213+
$itemKey = 'foo';
214+
$item = $pool->getItem($itemKey);
215+
$pool->save($item);
216+
217+
$anotherPool = $this->createCachePool();
218+
219+
$adapter = new FilesystemAdapter();
220+
$adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair
221+
222+
$this->assertFalse($anotherPool->hasItem($itemKey));
223+
}
224+
225+
public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemTags()
226+
{
227+
$pool = $this->createCachePool();
228+
229+
$itemKey = 'foo';
230+
$item = $pool->getItem($itemKey);
231+
$pool->save($item);
232+
233+
$anotherPool = $this->createCachePool();
234+
235+
$adapter = new FilesystemAdapter();
236+
$adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair
237+
238+
$item = $anotherPool->getItem($itemKey);
239+
$this->assertFalse($item->isHit());
240+
}
241+
242+
public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemAndOnlyHasTags()
243+
{
244+
$pool = $this->createCachePool();
245+
246+
$itemKey = 'foo';
247+
$item = $pool->getItem($itemKey);
248+
$pool->save($item);
249+
250+
$anotherPool = $this->createCachePool();
251+
252+
$adapter = new FilesystemAdapter();
253+
$adapter->deleteItem($itemKey); //simulate losing item but keeping tags
254+
255+
$this->assertFalse($anotherPool->hasItem($itemKey));
256+
}
257+
258+
public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags()
259+
{
260+
$pool = $this->createCachePool();
261+
262+
$itemKey = 'foo';
263+
$item = $pool->getItem($itemKey);
264+
$pool->save($item);
265+
266+
$anotherPool = $this->createCachePool();
267+
268+
$adapter = new FilesystemAdapter();
269+
$adapter->deleteItem($itemKey); //simulate losing item but keeping tags
270+
271+
$item = $anotherPool->getItem($itemKey);
272+
$this->assertFalse($item->isHit());
273+
}
274+
197275
/**
198276
* @return MockObject|PruneableCacheInterface
199277
*/

0 commit comments

Comments
 (0)
0