From 872494791ad13ce987f2bf2d72e26073a1114b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Neumaier?= Date: Thu, 14 Aug 2025 12:57:08 +0200 Subject: [PATCH] Add expiration to journal keys --- src/Caching/RedisJournal.php | 19 ++++++++++++++++--- tests/Cases/E2E/Predis.phpt | 22 +++++++++++++--------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Caching/RedisJournal.php b/src/Caching/RedisJournal.php index 631edea..7c24185 100644 --- a/src/Caching/RedisJournal.php +++ b/src/Caching/RedisJournal.php @@ -37,15 +37,28 @@ public function write(string $key, array $dependencies): void $this->client->multi(); + $usedKeys = []; + // add entry to each tag & tag to entry $tags = !isset($dependencies[Cache::Tags]) ? [] : (array) $dependencies[Cache::Tags]; foreach (array_unique($tags) as $tag) { - $this->client->sadd($this->formatKey($tag, self::SUFFIX_KEYS), [$key]); - $this->client->sadd($this->formatKey($key, self::SUFFIX_TAGS), [$tag]); + $usedKeys[] = $keySuffixKeys = $this->formatKey($tag, self::SUFFIX_KEYS); + $usedKeys[] = $keySuffixTags = $this->formatKey($key, self::SUFFIX_TAGS); + + $this->client->sadd($keySuffixKeys, [$key]); + $this->client->sadd($keySuffixTags, [$tag]); } if (isset($dependencies[Cache::Priority])) { - $this->client->zadd($this->formatKey(self::KEY_PRIORITY), [$key => (int) $dependencies[Cache::Priority]]); + $usedKeys[] = $keyPriority = $this->formatKey(self::KEY_PRIORITY); + + $this->client->zadd($keyPriority, [$key => (int) $dependencies[Cache::Priority]]); + } + + if (isset($dependencies[Cache::Expire])) { + foreach ($usedKeys as $usedKey) { + $this->client->expire($usedKey, (int) $dependencies[Cache::Expire]); + } } $this->client->exec(); diff --git a/tests/Cases/E2E/Predis.phpt b/tests/Cases/E2E/Predis.phpt index 61a5bdb..79f5b99 100644 --- a/tests/Cases/E2E/Predis.phpt +++ b/tests/Cases/E2E/Predis.phpt @@ -24,6 +24,13 @@ try { Environment::skip('Redis not found: ' . $e->getMessage()); } +// Helper function for journal related tests +$generateJournalKey = static function (string $key, string $suffix, bool $addStoragePrefix): string { + $prefix = $addStoragePrefix ? sprintf('%s:%s', RedisJournal::NS_PREFIX, RedisStorage::NS_PREFIX) : RedisJournal::NS_PREFIX; + + return sprintf('%s:%s:%s', $prefix, $key, $suffix); +}; + // Basic Toolkit::test(function () use ($cache): void { $cache->save('foo', 'bar'); @@ -93,11 +100,15 @@ Toolkit::test(function () use ($cache): void { }); // Expiration -Toolkit::test(function () use ($cache): void { - $cache->save('foo', 'bar', [Cache::EXPIRATION => 1]); +Toolkit::test(function () use ($cache, $client, $generateJournalKey): void { + $cache->save('foo', 'bar', [Cache::EXPIRATION => 1, Cache::TAGS => ['tag']]); Assert::same('bar', $cache->load('foo')); + Assert::same(1, $client->exists($generateJournalKey('foo', RedisJournal::SUFFIX_TAGS, true))); + Assert::same(1, $client->exists($generateJournalKey('tag', RedisJournal::SUFFIX_KEYS, false))); sleep(2); Assert::equal(null, $cache->load('foo')); + Assert::same(0, $client->exists($generateJournalKey('foo', RedisJournal::SUFFIX_TAGS, true))); + Assert::same(0, $client->exists($generateJournalKey('tag', RedisJournal::SUFFIX_KEYS, false))); }); // Priority @@ -130,13 +141,6 @@ Toolkit::test(function () use ($cache): void { Assert::same(null, $cache->load('foo4')); }); -// Helper function for journal related tests -$generateJournalKey = static function (string $key, string $suffix, bool $addStoragePrefix): string { - $prefix = $addStoragePrefix ? sprintf('%s:%s', RedisJournal::NS_PREFIX, RedisStorage::NS_PREFIX) : RedisJournal::NS_PREFIX; - - return sprintf('%s:%s:%s', $prefix, $key, $suffix); -}; - // Tags cleaning Toolkit::test(function () use ($storage, $client, $generateJournalKey): void { $storage->clean([Cache::ALL => true]);