Add CacheListener for observing cache events - #269
Open
Grryum wants to merge 2 commits into
Open
Conversation
kyri-petrou
reviewed
Jul 29, 2026
kyri-petrou
reviewed
Aug 13, 2026
Comment on lines
+315
to
+324
| var invalidated: List[Key] = Nil | ||
| val iterator = map.keySet().iterator() | ||
| while (iterator.hasNext) { | ||
| val key = iterator.next() | ||
| if (map.remove(key) ne null) { | ||
| invalidated = key :: invalidated | ||
| } | ||
| } | ||
| if (invalidated eq Nil) Exit.unit | ||
| else ZIO.foreachDiscard(invalidated)(key => trackEviction(key, CacheListener.EvictionCause.Invalidated)) |
Contributor
There was a problem hiding this comment.
I think this can be simplified / optimized to:
val invalidated = map.keySet()
map.clear()
if (invalidated.isEmpty) Exit.unit
else ZIO.foreachDiscard(invalidated)(key => trackEviction(key, CacheListener.EvictionCause.Invalidated))Note that we can also have a conditional if (isNoopListener) and simply do a map.clear() in that case to avoid the penalty of all the extra operations
Author
There was a problem hiding this comment.
ConcurrentHashMap#keySet() returns a live view backed by the map rather than a snapshot, so after map.clear() the invalidated set would be empty and no eviction events would be emitted. I kept the remove-and-check approach for a real listener, since it also ensures we only report entries actually removed by this invocation. I did add the suggested no-op fast path, which uses the original map.clear() directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Currently the only way to observe cache activity is polling
cacheStats, which exposes just cumulative hit/miss counters. There is no way to react to individual cache events, so common operational needs are impossible to express:invalidationEventsmethod to publish cache invalidation events. #57),Changes
This PR adds a
CacheListenerthat is notified of cache events:onLoadfires for every completed lookup (bothgetmisses andrefresh) with the resultingExitand the time the lookup took.onEvictioncarries the cause:Capacity,Expired, orInvalidated.CacheListener.metrics(cacheName)reports all events with ZIO metrics (zio_cache_hits,zio_cache_misses,zio_cache_load_successes,zio_cache_load_failures,zio_cache_load_duration,zio_cache_evictions), tagged with the cache name.Cache.make,makeWith, andmakeWithKey; withmakeWithKeythe listener observes the keys produced by thekeyByfunction.Design notes
getUnsafe, the eviction loop intrackAccess), where running effects would require either blocking the eviction loop on user code or giving up the zero-allocation hit path. This mirrors Caffeine'sStatsCounter. An effectful adapter (e.g. forking a handler on aRuntime) can be layered on top later without breaking anything.CacheImplementationclass (its constructor gained a parameter).invalidateAlldoes not emit per-key events since the underlying map is cleared wholesale; this is documented.