8000 [Cache] Add CacheItem::getTags() · symfony/symfony@7843ed3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7843ed3

Browse files
[Cache] Add CacheItem::getTags()
1 parent 635d77b commit 7843ed3

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TagAwareAdapter implements TagAwareAdapterInterface
2525
private $itemsAdapter;
2626
private $deferred = array();
2727
private $createCacheItem;
28+
private $setCacheItemTags;
2829
private $getTagsByKey;
2930
private $tagsAdapter;
3031

@@ -33,16 +34,29 @@ public function __construct(AdapterInterface $itemsAdapter, AdapterInterface $ta
3334
$this->itemsAdapter = $itemsAdapter;
3435
$this->tagsAdapter = $tagsAdapter ?: $itemsAdapter;
3536
$this->createCacheItem = \Closure::bind(
36-
function ($key, $value = null, CacheItem $protoItem = null) {
37+
function ($key, $value) {
3738
$item = new CacheItem();
3839
$item->key = $key;
3940
$item->value = $value;
40-
$item->isHit = false;
4141

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;
4660
}
4761

4862
return $item;
@@ -246,12 +260,12 @@ public function __destruct()
246260

247261
private function generateItems($items, array $tagKeys)
248262
{
249-
$bufferedItems = $itemTags = $invalidKeys = array();
250-
$f = $this->createCacheItem;
263+
$bufferedItems = $itemTags = array();
264+
$f = $this->setCacheItemTags;
251265

252266
foreach ($items as $key => $item) {
253267
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);
255269
continue;
256270
}
257271
if (!isset($tagKeys[$key])) {
@@ -260,24 +274,23 @@ private function generateItems($items, array $tagKeys)
260274
}
261275

262276
unset($tagKeys[$key]);
263-
if ($tags = $item->get()) {
264-
$itemTags[$key] = $tags;
265-
}
277+
$itemTags[$key] = $item->get() ?: array();
278+
266279
if (!$tagKeys) {
267280
$tagVersions = $this->getTagVersions($itemTags);
268281

269282
foreach ($itemTags as $key => $tags) {
270283
foreach ($tags as $tag => $version) {
271284
if ($tagVersions[$tag] !== $version) {
272-
$invalidKeys[$key] = true;
285+
unset($itemTags[$key]);
273286
continue 2;
274287
}
275288
}
276289
}
277-
$itemTags = $tagVersions = $tagKeys = null;
290+
$tagVersions = $tagKeys = null;
278291

279292
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);
281294
}
282295
$bufferedItems = null;
283296
}

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ final class CacheItem implements CacheItemInterface
2222
{
2323
protected $key;
2424
protected $value;
25-
protected $isHit;
25+
protected $isHit = false;
2626
protected $expiry;
2727
protected $defaultLifetime;
2828
protected $tags = array();
29+
protected $prevTags = array();
2930
protected $innerItem;
3031
protected $poolHash;
3132

@@ -130,6 +131,16 @@ public function tag($tags)
130131
return $this;
131132
}
132133

134+
/**
135+
* Returns the list of tags bound to value coming from the pool storage.
136+
*
137+
* @return array
138+
*/
139+
public function getPreviousTags()
140+
{
141+
return $this->prevTags;
142+
}
143+
133144
/**
134145
* Validates a cache key according to PSR-6.
135146
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,15 @@ public function testTagsAreCleanedOnDelete()
9797

9898
$this->assertTrue($pool->getItem('k')->isHit());
9999
}
100+
101+
public function testGetPreviousTags()
102+
{
103+
$pool = $this->createCachePool();
104+
105+
$i = $pool->getItem('k');
106+
$pool->save($i->tag('foo'));
107+
108+
$i = $pool->getItem('k');
109+
$this->assertSame(array('foo' => 'foo'), $i->getPreviousTags());
110+
}
100111
}

0 commit comments

Comments
 (0)
0