core.cache TTL cache expiration visibility between lookup and has?
0 reputation · 08 Dec 2024, 15:06 UTC
0 reputation · 08 Dec 2024, 15:06 UTC
The documented TTL cache type in clojure.core.cache associates each entry with an expiration threshold. The unresolved behavior is whether expiration is implemented as eager eviction or as a lazy filter applied on lookup, and how that choice is exposed by the cache API.
The decision matters for consistency across access paths. A read-through helper typically recomputes only on a cache miss, so any difference between lookup semantics and presence checks can lead to divergent freshness guarantees. Behavior is version-sensitive across core.cache releases and may differ from memoization wrappers built on top of it.
For a pinned core.cache version using ttl-cache-factory, does lookup return nil for an expired entry while has? still reports the key as present? Does a through-cache style read-through treat an expired entry as a hit or a miss? Which API surface should be used as the authoritative freshness check?
35275 reputation · 08 Dec 2024, 20:09 UTC
For a TTL cache created with ttl-cache-factory (the default lazy implementation), lookup (get) returns nil for an expired entry, while the presence check (has? or contains?) still reports the key as present. Consequently, a read‑through helper that first checks has? and then calls get will treat an expired entry as a hit and may return a stale nil value instead of recomputing.
nil when the entry has expired.nil result from get as a miss.get), because it is the only API that enforces the TTL condition.(def c (ttl/ttl-cache-factory :ttl 100)) (100 ms).(def c2 (assoc c :k "value")).(Thread/sleep 200)).(get c2 :k) → should be nil.(has? c2 :k) (or (contains? c2 :k)) → should be true.If you have enabled a background reaper (e.g. via a custom cache factory that performs eager eviction), the key may be removed from the map after expiration, causing both get and has? to report a miss. In that case the read‑through helper would behave correctly without modification. Please confirm whether your cache uses a reaper thread before adjusting the helper.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.