Skip to content
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

Bugfix: Validate tags on cache hit #127

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/Spiritix/LadaCache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ public function set($key, array $tags, $data)
}
}

/**
* Store given key in tags.
*
* Ensure key exists in the given tags.
* Redis sets are unique, so the same value will not be added twice.
*
* @param string $key
* @param array $tags
*/
public function setCacheTagsForKey($key, array $tags)
{
$key = $this->redis->prefix($key);
foreach ($tags as $tag) {
$this->redis->sadd($this->redis->prefix($tag), $key);
}
}

/**
* Returns value of a cached key.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Spiritix/LadaCache/QueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,13 @@ public function cacheQuery($queryClosure)

$action = ($result === null) ? 'Miss' : 'Hit';

// If not, execute the query closure and cache the result
if ($result === null) {
// Cache miss, execute the query closure and cache the result
$result = $queryClosure();
$this->cache->set($key, $tags, $result);
} else {
// Cache hit, validate cache tags on key
$this->cache->setCacheTagsForKey($key, $tags);
}

$this->destructCollector($reflector, $tags, $key, $action);
Expand Down
20 changes: 20 additions & 0 deletions tests/InvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@ public function testCacheStateWhenInvalideIsCalledInDistributedSystem(): void
$this->assertTrue($this->cache->has('key3'));
$this->assertTrue($this->cache->has('tag2'));
}

public function testCacheHitsValidateKeyInTags(): void
{
$this->cache->set('key1', ['tag2'], 'data'); // <-- Bug, simulate 'tag1' missing from key1
$this->cache->set('key2', ['tag1', 'tag2'], 'data');

// Simulate cache hit on Key1
// Start --------------------------------------------------------------------------------
$data = $this->cache->get('key1');
$this->assertSame('data', $data);
$this->cache->setCacheTagsForKey('key1', ['tag1', 'tag2']); // <-- Validate tags for key
// End ----------------------------------------------------------------------------------

// Invalidate cache.
$this->invalidator->invalidate(['tag1']);

// Check cache is invalidated:
$this->assertFalse($this->cache->has('key1'));
$this->assertFalse($this->cache->has('key2'));
}
}