Skip to content
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
17 changes: 8 additions & 9 deletions src/Spiritix/LadaCache/Invalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ public function __construct(Redis $redis)
*/
public function invalidate(array $tags)
{
$hashes = $this->getHashes($tags);

// Prefix tags in order to delete them
foreach ($tags as $key => $tag) {
$tags[$key] = $this->redis->prefix($tag);
}
$hashes = $this->getHashesAndDeleteTags($tags);

$this->deleteItems($hashes);
$this->deleteItems($tags);

return $hashes;
}
Expand All @@ -68,7 +62,7 @@ public function invalidate(array $tags)
*
* @return array
*/
private function getHashes(array $tags)
private function getHashesAndDeleteTags(array $tags)
{
$hashes = [];

Expand All @@ -79,7 +73,12 @@ private function getHashes(array $tags)
continue;
}

$hashes = array_merge($hashes, $this->redis->smembers($tag));
$transactionResult = $this->redis->transaction(function ($redis) use ($tag) {
$redis->smembers($tag);
$redis->del($tag);
});

$hashes = array_merge($hashes, $transactionResult[0]);
}

return array_unique($hashes);
Expand Down
50 changes: 50 additions & 0 deletions tests/InvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace Spiritix\LadaCache\Tests;

use ReflectionClass;
use Spiritix\LadaCache\Cache;
use Spiritix\LadaCache\Invalidator;

class InvalidatorTest extends TestCase
{
/**
* @var Cache
*/
private $cache;

/**
* @var Invalidator
*/
private $invalidator;

public function setUp(): void
Expand All @@ -14,6 +24,8 @@ public function setUp(): void

$this->cache = app()->make('lada.cache');
$this->invalidator = app()->make('lada.invalidator');

$this->cache->flush();
}

public function testInvalidate()
Expand Down Expand Up @@ -41,4 +53,42 @@ public function testInvalidateMultiTags()
$this->assertFalse($this->cache->has('tag1'));
$this->assertFalse($this->cache->has('tag2'));
}

public function testCacheStateWhenInvalideIsCalledInDistributedSystem(): void
{
// Make reflection class to call private methods.
$reflectionClass = new ReflectionClass($this->invalidator);
$getHashesAndDeleteTagsFunction = $reflectionClass->getMethod('getHashesAndDeleteTags');
$getHashesAndDeleteTagsFunction->setAccessible(true);

$deleteItemsFunction = $reflectionClass->getMethod('deleteItems');
$deleteItemsFunction->setAccessible(true);

$this->cache->set('key1', ['tag1'], 'data');
$this->cache->set('key2', ['tag2'], 'data');

$tags = ['tag1', 'tag2'];

// Replicate the functionality of the 'invalidate' function.
// Start --------------------------------------------------------------------------------
$hashes = $getHashesAndDeleteTagsFunction->invoke($this->invalidator, $tags);

$this->assertFalse($this->cache->has('tag2'));

// Simulate distributed system, that adds cache key in the middle of process.
// This added cache, has a tag that will be deleted by the first process.
$this->cache->set('key3', ['tag2'], 'data');

$deleteItemsFunction->invoke($this->invalidator, $hashes);

// End ----------------------------------------------------------------------------------

$this->assertFalse($this->cache->has('key1'));
$this->assertFalse($this->cache->has('key2'));
$this->assertFalse($this->cache->has('tag1'));

// Check key and tag from the simulate distributed system still exists.
$this->assertTrue($this->cache->has('key3'));
$this->assertTrue($this->cache->has('tag2'));
}
}