Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be6f57f

Browse files
[Cache] Add CacheItem::getPreviousTags()
1 parent b9b6ebd commit be6f57f

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

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

Lines changed: 29 additions & 10 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 $invalidateTags;
3031
private $tagsAdapter;
@@ -38,7 +39,6 @@ function ($key, $value, CacheItem $protoItem) {
3839
$item = new CacheItem();
3940
$item->key = $key;
4041
$item->value = $value;
41-
$item->isHit = false;
4242
$item->defaultLifetime = $protoItem->defaultLifetime;
4343
$item->expiry = $protoItem->expiry;
4444
$item->innerItem = $protoItem->innerItem;
@@ -49,6 +49,26 @@ function ($key, $value, CacheItem $protoItem) {
4949
null,
5050
CacheItem::class
5151
);
52+
$this->setCacheItemTags = \Closure::bind(
53+
function (CacheItem $item, $key, array &$itemTags) {
54+
if (!$item->isHit) {
55+
return $item;
56+
}
57+
if (isset($itemTags[$key])) {
58+
foreach ($itemTags[$key] as $tag => $version) {
59+
$item->prevTags[$tag] = $tag;
60+
}
61+
unset($itemTags[$key]);
62+
} else {
63+
$item->value = null;
64+
$item->isHit = false;
65+
}
66+
67+
return $item;
68+
},
69+
null,
70+
CacheItem::class
71+
);
5272
$this->getTagsByKey = \Closure::bind(
5373
function ($deferred) {
5474
$tagsByKey = array();
@@ -256,12 +276,12 @@ public function __destruct()
256276

257277
private function generateItems($items, array $tagKeys)
258278
{
259-
$bufferedItems = $itemTags = $invalidKeys = array();
260-
$f = $this->createCacheItem;
279+
$bufferedItems = $itemTags = array();
280+
$f = $this->setCacheItemTags;
261281

262282
foreach ($items as $key => $item) {
263283
if (!$tagKeys) {
264-
yield $key => isset($invalidKeys[self::TAGS_PREFIX.$key]) ? $f($key, null, $item) : $item;
284+
yield $key => $f($item, self::TAGS_PREFIX.$key, $itemTags);
265285
continue;
266286
}
267287
if (!isset($tagKeys[$key])) {
@@ -270,24 +290,23 @@ private function generateItems($items, array $tagKeys)
270290
}
271291

272292
unset($tagKeys[$key]);
273-
if ($tags = $item->get()) {
274-
$itemTags[$key] = $tags;
275-
}
293+
$itemTags[$key] = $item->get() ?: array();
294+
276295
if (!$tagKeys) {
277296
$tagVersions = $this->getTagVersions($itemTags);
278297

279298
foreach ($itemTags as $key => $tags) {
280299
foreach ($tags as $tag => $version) {
281300
if ($tagVersions[$tag] !== $version) {
282-
$invalidKeys[$key] = true;
301+
unset($itemTags[$key]);
283302
continue 2;
284303
}
285304
}
286305
}
287-
$itemTags = $tagVersions = $tagKeys = null;
306+
$tagVersions = $tagKeys = null;
288307

289308
foreach ($bufferedItems as $key => $item) {
290-
yield $key => isset($invalidKeys[self::TAGS_PREFIX.$key]) ? $f($key, null, $item) : $item;
309+
yield $key => $f($item, self::TAGS_PREFIX.$key, $itemTags);
291310
}
292311
$bufferedItems = null;
293312
}

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 the value coming from the pool storage if any.
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
@@ -114,4 +114,15 @@ public function testTagItemExpiry()
114114

115115
$this->assertFalse($pool->getItem('foo')->isHit());
116116
}
117+
118+
public function testGetPreviousTags()
119+
{
120+
$pool = $this->createCachePool();
121+
122+
$i = $pool->getItem('k');
123+
$pool->save($i->tag('foo'));
124+
125+
$i = $pool->getItem('k');
126+
$this->assertSame(array('foo' => 'foo'), $i->getPreviousTags());
127+
}
117128
}

0 commit comments

Comments
 (0)