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 922b7c1

Browse filesBrowse files
committed
bug #21338 [Cache] Fix tags expiration (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [Cache] Fix tags expiration | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21330 | License | MIT | Doc PR | - Commits ------- c0022f2 [Cache] Fix tags expiration
2 parents 6283fc4 + c0022f2 commit 922b7c1
Copy full SHA for 922b7c1

File tree

2 files changed

+43
-16
lines changed
Filter options

2 files changed

+43
-16
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
+26-16Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,23 @@ class TagAwareAdapter implements TagAwareAdapterInterface
2626
private $deferred = array();
2727
private $createCacheItem;
2828
private $getTagsByKey;
29+
private $invalidateTags;
2930
private $tagsAdapter;
3031

3132
public function __construct(AdapterInterface $itemsAdapter, AdapterInterface $tagsAdapter = null)
3233
{
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, CacheItem $protoItem) {
3738
$item = new CacheItem();
3839
$item->key = $key;
3940
$item->value = $value;
4041
$item->isHit = false;
41-
42-
if (null !== $protoItem) {
43-
$item->defaultLifetime = $protoItem->defaultLifetime;
44-
$item->innerItem = $protoItem->innerItem;
45-
$item->poolHash = $protoItem->poolHash;
46-
}
42+
$item->defaultLifetime = $protoItem->defaultLifetime;
43+
$item->expiry = $protoItem->expiry;
44+
$item->innerItem = $protoItem->innerItem;
45+
$item->poolHash = $protoItem->poolHash;
4746

4847
return $item;
4948
},
@@ -62,6 +61,20 @@ function ($deferred) {
6261
null,
6362
CacheItem::class
6463
);
64+
$this->invalidateTags = \Closure::bind(
65+
function (AdapterInterface $tagsAdapter, array $tags) {
66+
foreach ($tagsAdapter->getItems($tags) as $v) {
67+
$v->set(1 + (int) $v->get());
68+
$v->defaultLifetime = 0;
69+
$v->expiry = null;
70+
$tagsAdapter->saveDeferred($v);
71+
}
72+
73+
return $tagsAdapter->commit();
74+
},
75+
null,
76+
CacheItem::class
77+
);
6578
}
6679

6780
/**
@@ -74,13 +87,9 @@ public function invalidateTags(array $tags)
7487
$tags[$k] = $tag.static::TAGS_PREFIX;
7588
}
7689
}
90+
$f = $this->invalidateTags;
7791

78-
foreach ($this->tagsAdapter->getItems($tags) as $v) {
79-
$v->set(1 + (int) $v->get());
80-
$this->tagsAdapter->saveDeferred($v);
81-
}
82-
83-
return $this->tagsAdapter->commit();
92+
return $f($this->tagsAdapter, $tags);
8493
}
8594

8695
/**
@@ -211,22 +220,23 @@ public function commit()
211220
$ok = true;
212221

213222
if ($this->deferred) {
214-
foreach ($this->deferred as $key => $item) {
223+
$items = $this->deferred;
224+
foreach ($items as $key => $item) {
215225
if (!$this->itemsAdapter->saveDeferred($item)) {
216226
unset($this->deferred[$key]);
217227
$ok = false;
218228
}
219229
}
220230

221231
$f = $this->getTagsByKey;
222-
$tagsByKey = $f($this->deferred);
232+
$tagsByKey = $f($items);
223233
$deletedTags = $this->deferred = array();
224234
$tagVersions = $this->getTagVersions($tagsByKey);
225235
$f = $this->createCacheItem;
226236

227237
foreach ($tagsByKey as $key => $tags) {
228238
if ($tags) {
229-
$this->itemsAdapter->saveDeferred($f(static::TAGS_PREFIX.$key, array_intersect_key($tagVersions, $tags)));
239+
$this->itemsAdapter->saveDeferred($f(static::TAGS_PREFIX.$key, array_intersect_key($tagVersions, $tags), $items[$key]));
230240
} else {
231241
$deletedTags[] = static::TAGS_PREFIX.$key;
232242
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,21 @@ public function testTagsAreCleanedOnDelete()
9797

9898
$this->assertTrue($pool->getItem('k')->isHit());
9999
}
100+
101+
public function testTagItemExpiry()
102+
{
103+
$pool = $this->createCachePool(10);
104+
105+
$item = $pool->getItem('foo');
106+
$item->tag(array('baz'));
107+
$item->expiresAfter(100);
108+
109+
$pool->save($item);
110+
$pool->invalidateTags(array('baz'));
111+
$this->assertFalse($pool->getItem('foo')->isHit());
112+
113+
sleep(20);
114+
115+
$this->assertFalse($pool->getItem('foo')->isHit());
116+
}
100117
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.