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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"ext-redis": "*",
"utopia-php/validators": "^1.0 || ^2.0",
"utopia-php/console": "^0.1 || ^0.2",
"utopia-php/cache": "^6.1.1",
"utopia-php/cache": "^6.1.3",
"utopia-php/pools": "^2.0",
"utopia-php/mongo": "^1.5.4",
"utopia-php/query": "^0.7.2",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 6 additions & 11 deletions src/Database/Cache/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ public function loadMany(string $key, int $ttl, array $fields = []): array
#[\Override]
public function saveMany(string $key, array $data, int $ttl = 0): array|false
{
// Database caching does not batch-fill during the legacy pass.
if ($this->mode === 'legacy') {
throw new Unavailable('Legacy migration does not support batch fills');
}

return $this->cache->saveMany('v2:'.$key, $data, $ttl);
return $this->reader()->saveMany($this->key($key), $data, $ttl);
}

/** @return array{version: string, value: mixed} */
Expand Down Expand Up @@ -138,18 +133,18 @@ function () use ($key, $hash, &$result): void {
}

/** Rotate caller-owned withCache regions in both keyspaces, including when one is cold. */
public function rotate(string $key): void
public function rotate(string $key, int $ttl = 0): void
{
$this->both(
fn () => $this->rotateIn($this->legacy, $key),
fn () => $this->rotateIn($this->cache, 'v2:'.$key),
fn () => $this->rotateIn($this->legacy, $key, $ttl),
fn () => $this->rotateIn($this->cache, 'v2:'.$key, $ttl),
);
}

private function rotateIn(Cache $cache, string $key): void
private function rotateIn(Cache $cache, string $key, int $ttl): void
{
$cache->purge($key);
if ($cache->save($key, \bin2hex(\random_bytes(16))) === false) {
if ($cache->save($key, \bin2hex(\random_bytes(16)), ttl: $ttl) === false) {
throw new Unavailable('Failed to rotate cache region');
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/Cache/BridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ public function testRapidWritesWithCloudsGraceWindowKeepBothProtocolsUsable(): v
}
}

public function testApplicationListCacheSupportsExpiringVersionsAndBatchFillsInEveryPhase(): void
{
foreach (['legacy', 'dual', 'v2'] as $mode) {
$cache = new Bridge($this->cache, $mode);
$key = $this->namespace.':lists:'.$mode;
$this->assertSame('version', $cache->save($key.':version', 'version', ttl: 60));
$this->assertSame('version', $cache->load($key.':version', 60));
$wireKey = $mode === 'legacy' ? $key : 'v2:'.$key;
$this->assertGreaterThan(0, $this->redis->ttl($wireKey.':version'));
$cache->rotate($key.':version', 60);
$this->assertNotSame('version', $cache->load($key.':version', 60));
$this->assertGreaterThan(0, $this->redis->ttl($wireKey.':version'));
$fields = ['documents' => ['row'], 'total' => '1'];
$this->assertSame($fields, $cache->saveMany($key, $fields));
$this->assertSame($fields, $cache->loadMany($key, 60));
$cache->purge($key);
$this->assertSame([], $cache->loadMany($key, 60));
}
}

public function testSlotZeroRemainsCacheableAcrossEveryPhase(): void
{
foreach (['', 'legacy', 'dual', 'v2'] as $mode) {
Expand Down
Loading