We're updating the issue view to help you get more done. 

Stats for cache


I have used core.cache in some of my projects. It is a great library and I liked it a lot. It only lacks stats so I can adjust size of the cache based on some statistics. I have decided to write a custom cache implementation that would allow to get hit/miss stats for the cache. First it was a separate project but then I figured out that I need one change in core.cache itself which I can not hack nicely around. So here is the patch to add stats-aware cache implementation.

Quick demo:

1 2 3 4 5 6 7 8 9 10 11 (require '[clojure.core.cache :as core.cache] '[clojure.core.cache.stats :as ccs] '[clojure.core.cache.stats.counters :as ccs.counters]) (def cache (-> {} core.cache/lru-cache-factory ccs/measured-cache)) (ccs/stats cache) ; {:hit 0, :miss 0, :request 0, :hit-ratio 1.0, :miss-ratio 1.0} (def cache (assoc cache :foo "bar")) (ccs/stats cache) ; {:hit 0, :miss 1, :request 1, :hit-ratio 0.0, :miss-ratio 1.0} (get cache :foo) ; "bar" (get cache :foo) ; "bar" (ccs/stats cache) ; {:hit 2, :miss 1, :request 3, :hit-ratio 0.6666666666666666, :miss-ratio 0.3333333333333333}

What's new:

  • core.cache.stats namespace which provides MeasuredCache that implements CacheProtocol and a measured-cache function to instantiate it. MeasuredCache also implements MeasuredCacheProtocol which has only one responsibility - to return snapshot of hit/miss stats

  • core.cache.stats.counters namespace which provides a protocol (StatsCounterProtocol) that allows to implement hit/miss counters. There are two implementations already: one is based on long wrapped in atom, another one (which is used by default) is based on LongAdder

  • tests are there for all the methods that could be invoked

Caveats (in no particular order):

Things to finish before merge:

  • if you would like the idea I'll add more docs (maybe I should have started with this one so higher chances to be accepted?)

  • polish code (I'm not an expert in Clojure at all), naming and namespaces

Thank you in advance for the feedback





Sean Corfield


Oleksii Khomchenko






Code and Test