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

Skip to content

Commit 11ed156

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

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-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->tags[$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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ 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();
2929
protected $innerItem;
3030
protected $poolHash;
31+
protected $resetTags = true;
3132

3233
/**
3334
* {@inheritdoc}
@@ -58,6 +59,10 @@ public function isHit()
5859
*/
5960
public function set($value)
6061
{
62+
if ($this->resetTags) {
63+
$this->resetTags = false;
64+
$this->tags = array();
65+
}
6166
$this->value = $value;
6267

6368
return $this;
@@ -108,6 +113,10 @@ public function expiresAfter($time)
108113
*/
109114
public function tag($tags)
110115
{
116+
if ($this->resetTags) {
117+
$this->resetTags = false;
118+
$this->tags = array();
119+
}
111120
if (!is_array($tags)) {
112121
$tags = array($tags);
113122
}
@@ -130,6 +139,16 @@ public function tag($tags)
130139
return $this;
131140
}
132141

142+
/**
143+
* Returns the list of tags bound to the value of the current cache item.
144+
*
145+
* @return array
146+
*/
147+
public function getTags()
148+
{
149+
return $this->tags;
150+
}
151+
133152
/**
134153
* Validates a cache key according to PSR-6.
135154
*

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

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

9898
$this->assertTrue($pool->getItem('k')->isHit());
9999
}
100+
101+
public function testTagsAreCleanedOnSet()
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->getTags());
110+
$pool->save($i->set('bar'));
111+
112+
$pool->invalidateTags(array('foo'));
113+
$this->assertTrue($pool->getItem('k')->isHit());
114+
$this->assertSame(array(), $i->getTags());
115+
}
116+
117+
public function testTagsAreEmptyOnMiss()
118+
{
119+
$basePool = new FilesystemAdapter();
120+
$tagPool = new TagAwareAdapter($basePool);
121+
122+
$i = $tagPool->getItem('k');
123+
$tagPool->save($i->tag('foo'));
124+
125+
$i = $tagPool->getItem('k');
126+
$this->assertSame(array('foo' => 'foo'), $i->getTags());
127+
128+
$basePool->deleteitem('k');
129+
$i = $tagPool->getItem('k');
130+
$this->assertSame(array(), $i->getTags());
131+
}
100132
}

0 commit comments

Comments
 (0)