Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da35466

Browse filesBrowse files
[Cache] Add CacheItem::getPreviousTags()
1 parent 2a99e16 commit da35466
Copy full SHA for da35466

File tree

4 files changed

+55
-11
lines changed
Filter options

4 files changed

+55
-11
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
+29-10Lines 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/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* [EXPERIMENTAL] added CacheItem::getPreviousTags() to get bound tags coming from the pool storage if any
78
* added PSR-16 "Simple Cache" implementations for all existing PSR-6 adapters
89
* added Psr6Cache and SimpleCacheAdapter for bidirectional interoperability between PSR-6 and PSR-16
910
* added MemcachedAdapter (PSR-6) and MemcachedCache (PSR-16)

‎src/Symfony/Component/Cache/CacheItem.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/CacheItem.php
+14-1Lines changed: 14 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,18 @@ 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+
* @experimental in version 3.3
140+
*/
141+
public function getPreviousTags()
142+
{
143+
return $this->prevTags;
144+
}
145+
133146
/**
134147
* Validates a cache key according to PSR-6.
135148
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
+11Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.