Repository navigation
[release] feat(micronaut-cache-redis): list cache keys portably with ListableCache (0.4.0) - #134
Conversation
…che (0.4.0) Micronaut's SyncCache cannot enumerate its keys, so a caller that needs to walk a cache (e.g. a periodic sweep over a small registry) had to reach for the backend directly. ListableCache adds that one capability behind a portable entry point: - ListableCache.keys() returns the keys a cache holds, in string form - RedisCache implements it with the SCAN walk invalidateAll() already used, now shared by both; keys are returned without the <cacheName>: prefix and de-duplicated. Weakly consistent; on a multi-shard cluster-mode server SCAN only sees one node's keys (same limit as invalidateAll()) - ListableCache.of(SyncCache) returns the RedisCache itself, a view over the native cache for Micronaut's Caffeine cache, or empty otherwise, so the same caller code runs with or without Redis. Caffeine is a compileOnly dependency loaded only after a class-presence check Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
|
@claude review |
|
Claude finished @pditommaso's task in 2m 43s —— View job Review:
|
…back Review of #134: Micronaut's Caffeine cache keeps the caller's key object, so a non-string key is listed as its toString(), which does not read the entry back (and equal toString() values collapse), while the Redis cache stores every key in string form and round-trips. Say so in ListableCache.keys() and the README, along with keys() building the whole set in memory (small caches only). Give the list-cache test fixture a TTL like its neighbours, so its keys don't outlive the spec in the shared container. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
…tainer Review of #134: only the RedisCache case of ListableCache.of needs Redis, yet the whole spec started a container. That case moves to RedisCacheTest, which already runs one; ListableCacheTest keeps the Caffeine and non-listable cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
|
Addressed the review in #134 (comment):
🤖 Generated with Claude Code |
Summary
Micronaut's
SyncCachecan read, write and invalidate entries, but it can't list them. Code that needs to walk a small cache, for example a periodic sweep over a registry, has had to bypass the cache and talk to Redis directly. Then it no longer works without Redis, e.g. with the default in-process cache for local runs and tests.This adds that one missing capability behind a portable entry point, so the same code works whether the cache is backed by Redis or by Micronaut's Caffeine cache:
First consumer: the sched VM workload-identity GC. It records each use of an AWS VM instance role in a Micronaut cache, and a 6-hourly sweep lists that cache to find roles left unused for 30 days. The sched PR follows, stacked on seqeralabs/sched#1415.
What changes
io.seqera.cache.ListableCache. One method,Set<String> keys(), which returns the keys a cache holds in string form.RedisCacheimplements it. It walks the cache's keys with the sameSCAN MATCH <cacheName>:*loopinvalidateAll()already uses. That loop now lives in one private helper shared by both methods. Keys come back without the<cacheName>:prefix the default key serializer adds, with duplicates removed (SCANcan return a key more than once).invalidateAll()behaves as before; the CROSSSLOT-safe pipelined deletes from 0.3.1 are unchanged.ListableCache.of(SyncCache)returns:RedisCacheitself;Optional.empty()for any other cache, so callers can decide to stay off.compileOnlydependency. Its classes are only touched after a class-presence check, and the Caffeine-typed code sits in a nested class the JVM loads only on that path. Consumers that only use Redis don't pull Caffeine in.VERSIONis 0.4.0 (a minor bump, additive only);changelog.txtandREADME.mdare updated (new "Listing Keys" section, and the stale1.0.0install coordinate fixed).Semantics and limits (documented in the Javadoc and README)
SCANreads every key and filters by prefix afterwards, exactly likeinvalidateAll(). Batch size isinvalidate-scan-count.SCANon one connection only sees one node's keys, the same limitinvalidateAll()already has. With a single shard the listing is complete.getKeysPattern()/invalidateAll()already do.Test evidence
./gradlew :micronaut-cache-redis:test, 46 tests, 0 failures (Redis 7.0.4 Testcontainer)invalidateAll()tests, including the 100-key multi-page scan, still pass after the loop was sharedNew tests (each was run against a stub and seen failing first):
RedisCacheTest:SCANpages (scan count 2);ListableCacheTest:of(RedisCache)returns the cache itself;of(...)lists the keys of a real Micronaut Caffeine cache (micronaut-cache-caffeine4.3.1);of(...)returns empty for a cache it can't enumerate.Also exercised end to end from sched through
includeBuild: sched'sRedisCacheSerializationTestruns the productionredisprofile against a Redis container. It records, lists and reads back an entry through a realRedisCachewithJacksonObjectSerializer, and sched's full test suite passes against this build.Note:
./gradlew :micronaut-cache-redis:javadocalready fails onmaster("malformed HTML" on the@author … <email>lines inRedisCache,CacheValueEncryptor, …). The new file follows the same convention; this PR doesn't change that.Release
The title carries
[release], so merging publishesmicronaut-cache-redis0.4.0. sched will then drop its temporaryincludeBuild('../libseqera')and consume the published artifact.🤖 Generated with Claude Code