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
24 changes: 17 additions & 7 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8554,7 +8554,7 @@ protected function purgeCachedDocumentInternal(string $collectionId, ?string $id
return true;
}

[$collectionKey, $documentKey] = $this->getCacheKeys($collectionId, $id);
[$collectionKey, $documentKey] = $this->getCacheBaseKeys($collectionId, $id);

$this->cache->purge($collectionKey, $documentKey);
$this->cache->purge($documentKey);
Expand Down Expand Up @@ -9927,10 +9927,9 @@ public function getSchemaIndexes(string $collection): array
/**
* @param string $collectionId
* @param string|null $documentId
* @param array<string> $selects
* @return array{0: string, 1: string, 2: string}
* @return array{0: string, 1: string}
*/
public function getCacheKeys(string $collectionId, ?string $documentId = null, array $selects = []): array
public function getCacheBaseKeys(string $collectionId, ?string $documentId = null): array
{
if ($this->adapter->getSupportForHostname()) {
$hostname = $this->adapter->getHostname();
Expand All @@ -9955,9 +9954,20 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
$collectionId
);

if ($documentId) {
$documentKey = $documentHashKey = "{$collectionKey}:{$documentId}";
return [$collectionKey, $documentId ? "{$collectionKey}:{$documentId}" : ''];
}

/**
* @param string $collectionId
* @param string|null $documentId
* @param array<string> $selects
* @return array{0: string, 1: string, 2: string}
*/
public function getCacheKeys(string $collectionId, ?string $documentId = null, array $selects = []): array
{
[$collectionKey, $documentKey] = $this->getCacheBaseKeys($collectionId, $documentId);

if ($documentId) {
$sortedSelects = $selects;
\sort($sortedSelects);

Expand All @@ -9971,7 +9981,7 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a

return [
$collectionKey,
$documentKey ?? '',
$documentKey,
$documentHashKey ?? ''
];
}
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/CacheKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ private function getHashKey(Database $db, string $collection = 'col', string $do
return $hashKey;
}

public function testBaseKeysMatchScopedVariantKeys(): void
{
$adapter = $this->createMock(Adapter::class);
$adapter->method('getSupportForHostname')->willReturn(true);
$adapter->method('getHostname')->willReturn('mysql-project');
$adapter->method('getNamespace')->willReturn('project');
$adapter->method('getTenant')->willReturn(42);
$adapter->method('getSharedTables')->willReturn(true);

$db = new Database($adapter, new Cache(new None()));
$db->setGlobalCollections(['global']);

foreach ([['col', 'doc'], [Database::METADATA, 'col'], [Database::METADATA, 'global']] as [$collection, $document]) {
$full = $db->getCacheKeys($collection, $document, ['name']);
$withoutHash = $db->getCacheBaseKeys($collection, $document);

$this->assertSame([$full[0], $full[1]], $withoutHash);
$this->assertNotSame('', $full[2]);
}
}

public function testSameConfigProducesSameCacheKey(): void
{
$db1 = $this->createDatabase();
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/ForUpdateCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ private function staleCache(string $attribute, string $value): void
$this->adapter->updateDocument($collection, 'project', $document, true);
}

public function testPurgeMakesTheNextReadReturnFreshData(): void
{
$this->assertSame('stale', $this->database->getDocument('projects', 'project')->getAttribute('name'));
$this->staleCache('name', 'fresh');
$this->assertSame('stale', $this->database->getDocument('projects', 'project')->getAttribute('name'));

$this->database->purgeCachedDocument('projects', 'project');

$this->assertSame('fresh', $this->database->getDocument('projects', 'project')->getAttribute('name'));
}

public function testForUpdateReadBypassesStaleCache(): void
{
$cached = $this->database->getDocument('projects', 'project');
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/WithCacheLeaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ protected function setUp(): void
$this->key = $this->database->getQueryCacheKey('projects');
}

public function testDocumentPurgeRemovesAllVariantsAndRejectsStaleWrites(): void
{
[$collectionKey, $documentKey, $firstHash] = $this->database->getCacheKeys('projects', 'project');
[, , $secondHash] = $this->database->getCacheKeys('projects', 'project', ['name']);
$this->cacheAdapter->save($collectionKey, 'legacy', $documentKey);
$this->cacheAdapter->save($documentKey, 'first', $firstHash);
$this->cacheAdapter->save($documentKey, 'second', $secondHash);
$lease = $this->cacheAdapter->getGeneration($documentKey);

$this->assertTrue($this->database->purgeCachedDocument('projects', 'project'));

$this->assertFalse($this->cacheAdapter->load($collectionKey, Database::TTL, $documentKey));
$this->assertFalse($this->cacheAdapter->load($documentKey, Database::TTL, $firstHash));
$this->assertFalse($this->cacheAdapter->load($documentKey, Database::TTL, $secondHash));
$this->cacheAdapter->saveWithLease($documentKey, 'stale', $firstHash, $lease);
$this->assertFalse($this->cacheAdapter->load($documentKey, Database::TTL, $firstHash));
$this->assertSame('fresh', $this->database->getDocument('projects', 'project')->getAttribute('name'));
}

public function testStaleListWriteAfterConcurrentPurgeIsRejected(): void
{
$hash = 'list-hash';
Expand Down
Loading