#485·ristretto

Allow controlling whether a cache hit is counted as hit or miss based on the contents of the cached value

Author: CAFxXCreated Mar 31, 2026Updated Jun 24, 2026
LabelsStale

Is your feature request related to a problem? Please describe

I have a use case where I need to consider a Get a hit not only if the key is in the cache, but also if the value matches certain conditions (per-request freshness compatibility). Basically if a request specifies maxStaleness=x and the value in the cache was fetched more than x ago, then it is considered stale for that request (but a later request with a higher maxStaleness may consider it still valid)

Describe the solution you'd like

Either:

  • A way to pass a condition callback to a Get-like function so that I can do: v, found := r.GetXxx(k, func(v V) bool { return time.Since(v.fetchedAt) < maxStaleness })
  • A Peek(K) (V, bool) function that does not update the hit/miss count, and a HitMiss(K, bool) that should be called later, to record either a hit or miss so that I can do: v, found := r.Peek(k); r.HitMiss(k, time.Since(v.fetchedAt) < maxStaleness))

(method names are just placeholders, could not come up with good names on the spot)

Describe alternatives you've considered

Additional context