Skip to content

[Cache] TagAwareAdapterInterface::invalidateTags() should commit deferred items #27007

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

Merged
merged 1 commit into from
Apr 29, 2018
Merged
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
123 changes: 81 additions & 42 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ class TagAwareAdapter implements TagAwareAdapterInterface, PruneableInterface, R
private $getTagsByKey;
private $invalidateTags;
private $tags;
private $knownTagVersions = array();
private $knownTagVersionsTtl;

public function __construct(AdapterInterface $itemsPool, AdapterInterface $tagsPool = null)
public function __construct(AdapterInterface $itemsPool, AdapterInterface $tagsPool = null, $knownTagVersionsTtl = 0.15)
{
$this->pool = $itemsPool;
$this->tags = $tagsPool ?: $itemsPool;
$this->knownTagVersionsTtl = $knownTagVersionsTtl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should we use this property when we always want to fetch the tag versions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't 0 work? or is the check L351 wrong? PR welcome if you found a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 didn't fix it for me, seems like the check is wrong.
Filling in 1000 did work.
I'll look into it a bit better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a PR for this #33754

$this->createCacheItem = \Closure::bind(
function ($key, $value, CacheItem $protoItem) {
$item = new CacheItem();
Expand Down Expand Up @@ -87,8 +90,7 @@ function ($deferred) {
);
$this->invalidateTags = \Closure::bind(
function (AdapterInterface $tagsAdapter, array $tags) {
foreach ($tagsAdapter->getItems($tags) as $v) {
$v->set(1 + (int) $v->get());
foreach ($tags as $v) {
$v->defaultLifetime = 0;
$v->expiry = null;
$tagsAdapter->saveDeferred($v);
Expand All @@ -106,14 +108,42 @@ function (AdapterInterface $tagsAdapter, array $tags) {
*/
public function invalidateTags(array $tags)
{
foreach ($tags as $k => $tag) {
if ('' !== $tag && \is_string($tag)) {
$tags[$k] = $tag.static::TAGS_PREFIX;
$ok = true;
$tagsByKey = array();
$invalidatedTags = array();
foreach ($tags as $tag) {
CacheItem::validateKey($tag);
$invalidatedTags[$tag] = 0;
}

if ($this->deferred) {
$items = $this->deferred;
foreach ($items as $key => $item) {
if (!$this->pool->saveDeferred($item)) {
unset($this->deferred[$key]);
$ok = false;
}
}

$f = $this->getTagsByKey;
$tagsByKey = $f($items);
$this->deferred = array();
}

$tagVersions = $this->getTagVersions($tagsByKey, $invalidatedTags);
$f = $this->createCacheItem;

foreach ($tagsByKey as $key => $tags) {
$this->pool->saveDeferred($f(static::TAGS_PREFIX.$key, array_intersect_key($tagVersions, $tags), $items[$key]));
}
$ok = $this->pool->commit() && $ok;

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

return $f($this->tags, $tags);
return $ok;
}

/**
Expand All @@ -132,7 +162,7 @@ public function hasItem($key)
}

foreach ($this->getTagVersions(array($itemTags)) as $tag => $version) {
if ($itemTags[$tag] !== $version) {
if ($itemTags[$tag] !== $version && 1 !== $itemTags[$tag] - $version) {
return false;
}
}
Expand Down Expand Up @@ -241,29 +271,7 @@ public function saveDeferred(CacheItemInterface $item)
*/
public function commit()
{
$ok = true;

if ($this->deferred) {
$items = $this->deferred;
foreach ($items as $key => $item) {
if (!$this->pool->saveDeferred($item)) {
unset($this->deferred[$key]);
$ok = false;
}
}

$f = $this->getTagsByKey;
$tagsByKey = $f($items);
$this->deferred = array();
$tagVersions = $this->getTagVersions($tagsByKey);
$f = $this->createCacheItem;

foreach ($tagsByKey as $key => $tags) {
$this->pool->saveDeferred($f(static::TAGS_PREFIX.$key, array_intersect_key($tagVersions, $tags), $items[$key]));
}
}

return $this->pool->commit() && $ok;
return $this->invalidateTags(array());
}

public function __destruct()
Expand Down Expand Up @@ -294,7 +302,7 @@ private function generateItems($items, array $tagKeys)

foreach ($itemTags as $key => $tags) {
foreach ($tags as $tag => $version) {
if ($tagVersions[$tag] !== $version) {
if ($tagVersions[$tag] !== $version && 1 !== $version - $tagVersions[$tag]) {
unset($itemTags[$key]);
continue 2;
}
Expand All @@ -310,23 +318,54 @@ private function generateItems($items, array $tagKeys)
}
}

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

foreach ($tagsByKey as $tags) {
$tagVersions += $tags;
}

if ($tagVersions) {
$tags = array();
foreach ($tagVersions as $tag => $version) {
$tagVersions[$tag] = $tag.static::TAGS_PREFIX;
$tags[$tag.static::TAGS_PREFIX] = $tag;
if (!$tagVersions) {
return array();
}

if (!$fetchTagVersions = 1 !== \func_num_args()) {
foreach ($tagsByKey as $tags) {
foreach ($tags as $tag => $version) {
if ($tagVersions[$tag] > $version) {
$tagVersions[$tag] = $version;
}
}
}
}

$now = microtime(true);
$tags = array();
foreach ($tagVersions as $tag => $version) {
$tags[$tag.static::TAGS_PREFIX] = $tag;
if ($fetchTagVersions || !isset($this->knownTagVersions[$tag])) {
continue;
}
$version -= $this->knownTagVersions[$tag][1];
if ((0 !== $version && 1 !== $version) || $this->knownTagVersionsTtl > $now - $this->knownTagVersions[$tag][0]) {
// 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;
}
foreach ($this->tags->getItems($tagVersions) as $tag => $version) {
$tagVersions[$tags[$tag]] = $version->get() ?: 0;
}

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

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]);
}
$this->knownTagVersions[$tag] = array($now, $tagVersions[$tag]);
}

return $tagVersions;
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ public function testInvalidateTags()
$this->assertTrue($pool->getItem('foo')->isHit());
}

public function testInvalidateCommits()
{
$pool1 = $this->createCachePool();

$foo = $pool1->getItem('foo');
$foo->tag('tag');

$pool1->saveDeferred($foo->set('foo'));
$pool1->invalidateTags(array('tag'));

$pool2 = $this->createCachePool();
$foo = $pool2->getItem('foo');

$this->assertTrue($foo->isHit());
}

public function testTagsAreCleanedOnSave()
{
$pool = $this->createCachePool();
Expand Down