Skip to content

Commit d21b396

Browse files
committed
fold enable cache lua inside of redis store
1 parent 21e0267 commit d21b396

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/CacheManager.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use NormCache\Planning\TableIdentityResolver;
1010
use NormCache\Support\CacheKeyBuilder;
1111
use NormCache\Support\QueryIdentity;
12-
use NormCache\Support\RedisScripts;
1312
use NormCache\Support\RedisStore;
1413
use NormCache\Values\CacheConfig;
1514
use NormCache\Values\TableIdentity;
@@ -133,12 +132,10 @@ public function enableCache(): ?int
133132
$this->runtime->forgetEpoch();
134133

135134
try {
136-
$epoch = $this->store->script(
137-
RedisScripts::get('enable_cache'),
138-
[$this->keys->epoch(), $this->keys->disabled()],
135+
return $this->store->enableCache(
136+
$this->keys->epoch(),
137+
$this->keys->disabled(),
139138
);
140-
141-
return (int) $epoch;
142139
} catch (\Throwable $exception) {
143140
$this->runtime->fail($exception);
144141

src/Support/RedisStore.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ public function invalidateTableState(
254254
);
255255
}
256256

257+
public function enableCache(string $epochKey, string $disabledKey): int
258+
{
259+
return (int) $this->script(
260+
RedisScripts::get('enable_cache'),
261+
[$epochKey, $disabledKey],
262+
);
263+
}
264+
257265
public function brpop(string $key, float $timeoutSeconds): bool
258266
{
259267
return $this->withRawValues(static function (Connection $connection) use ($key, $timeoutSeconds): bool {
@@ -269,7 +277,7 @@ public function brpop(string $key, float $timeoutSeconds): bool
269277
* @param list<string> $keys
270278
* @param list<mixed> $args
271279
*/
272-
public function script(string $script, array $keys, array $args = []): mixed
280+
private function script(string $script, array $keys, array $args = []): mixed
273281
{
274282
$connection = $this->connection();
275283
$keyCount = count($keys);

tests/Integration/RuntimeKillSwitchTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ public function test_enable_advances_the_epoch_and_clears_the_flag(): void
7676
$this->assertFalse(NormCache::cacheDisabled());
7777
}
7878

79+
public function test_store_enable_returns_the_advanced_epoch_and_clears_the_flag(): void
80+
{
81+
$epochKey = $this->cacheKeys()->epoch();
82+
$disabledKey = $this->cacheKeys()->disabled();
83+
$this->assertTrue(NormCache::disableCache());
84+
$before = (int) ($this->cacheStore()->getRaw($epochKey) ?? '0');
85+
$this->assertNotNull($this->cacheStore()->getRaw($disabledKey));
86+
87+
$epoch = $this->cacheStore()->enableCache($epochKey, $disabledKey);
88+
89+
$this->assertSame($before + 1, $epoch);
90+
$this->assertSame((string) ($before + 1), $this->cacheStore()->getRaw($epochKey));
91+
$this->assertNull($this->cacheStore()->getRaw($disabledKey));
92+
}
93+
7994
public function test_payloads_cached_before_a_disable_are_not_served_after_enable(): void
8095
{
8196
$read = fn() => DB::table('posts')->where('id', 1)->value('title');

0 commit comments

Comments
 (0)