8000 [Cache] Decrease the probability of invalidation loss on tag eviction by sbelyshkin · Pull Request #43301 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Decrease the probability of invalidation loss on tag eviction #43301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ public function invalidateTags(array $tags)
}
$ok = $this->pool->commit() && $ok;

if ($invalidatedTags) {
$ok = (self::$invalidateTags)($this->tags, $invalidatedTags) && $ok;
}

return $ok;
}

Expand Down Expand Up @@ -175,7 +171,7 @@ public function hasItem($key)
}

foreach ($this->getTagVersions([$itemTags]) as $tag => $version) {
if ($itemTags[$tag] !== $version && 1 !== $itemTags[$tag] - $version) {
if ($itemTags[$tag] !== $version && $itemTags[$tag] !== $version + 1 && (\PHP_INT_MIN !== $itemTags[$tag] || \PHP_INT_MAX !== $version)) {
return false;
}
}
Expand Down Expand Up @@ -357,7 +353,7 @@ private function generateItems(iterable $items, array $tagKeys): \Generator

foreach ($itemTags as $key => $tags) {
foreach ($tags as $tag => $version) {
if ($tagVersions[$tag] !== $version && 1 !== $version - $tagVersions[$tag]) {
if ($tagVersions[$tag] !== $version && $tagVersions[$tag] + 1 !== $version && (\PHP_INT_MAX !== $tagVersions[$tag] || \PHP_INT_MIN !== $version)) {
unset($itemTags[$key]);
continue 2;
}
Expand All @@ -373,7 +369,7 @@ private function generateItems(iterable $items, array $tagKeys): \Generator
}
}

private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
private function getTagVersions(array $tagsByKey, array $invalidatedTags = [])
{
$tagVersions = $invalidatedTags;

Expand All @@ -388,7 +384,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
if (!$fetchTagVersions = 1 !== \func_num_args()) {
foreach ($tagsByKey as $tags) {
foreach ($tags as $tag => $version) {
if ($tagVersions[$tag] > $version) {
if ($tagVersions[$tag] > $version || $version > 0 && $version - $tagVersions[$tag] > \PHP_INT_MAX) {
$tagVersions[$tag] = $version;
}
}
Expand All @@ -403,27 +399,43 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
$fetchTagVersions = true;
continue;
}
$version -= $this->knownTagVersions[$tag][1];
if ((0 !== $version && 1 !== $version) || $now - $this->knownTagVersions[$tag][0] >= $this->knownTagVersionsTtl) {

if (($knownTagVersion = $this->knownTagVersions[$tag][1]) !== $version && $knownTagVersion + 1 !== $version && (\PHP_INT_MAX !== $knownTagVersion || \PHP_INT_MIN !== $version)
|| $now - $this->knownTagVersions[$tag][0] >= $this->knownTagVersionsTtl
< F15E /td> ) {
// reuse previously fetched tag versions up to the ttl, unless we are storing items or a potential miss arises
$fetchTagVersions = true;
} else {
$this->knownTagVersions[$tag][1] += $version;
$this->knownTagVersions[$tag][1] = $version;
}
}

if (!$fetchTagVersions) {
return $tagVersions;
}

$updatedTags = [];
foreach ($this->tags->getItems(array_keys($tags)) as $tag => $version) {
$tagVersions[$tag = $tags[$tag]] = $version->get() ?: 0;
if (isset($invalidatedTags[$tag])) {
$invalidatedTags[$tag] = $version->set(++$tagVersions[$tag]);
$tag = $tags[$tag];

if ($version->isHit()) {
$tagVersions[$tag] = $version->get();
} else {
$tagVersions[$tag] = mt_rand(\PHP_INT_MIN, \PHP_INT_MAX);
$updatedTags[$tag] = $version->set($tagVersions[$tag]);
}

if (isset($invalidatedTags[$tag]) && !isset($updatedTags[$tag])) {
$tagVersions[$tag] = $tagVersions[$tag] < \PHP_INT_MAX ? 1 + $tagVersions[$tag] : \PHP_INT_MIN;
$updatedTags[$tag] = $version->set($tagVersions[$tag]);
}
$this->knownTagVersions[$tag] = [$now, $tagVersions[$tag]];
}

if ($updatedTags) {
(self::$invalidateTags)($this->tags, $updatedTags);
}

return $tagVersions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function testKnownTagVersionsTtl()
$item->expiresAfter(100);

$tag = $this->createMock(CacheItemInterface::class);
$tag->expects(self::exactly(2))->method('isHit')->willReturn(true);
$tag->expects(self::exactly(2))->method('get')->willReturn(10);

$tagsPool->expects(self::exactly(2))->method('getItems')->willReturn([
Expand Down
0