Welcome! Please see the About page for a little more info on how this works.

0 votes
in core.cache by
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:

{code:none}
(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):

* LongAdderStatsCounter introduces hard dependency on LongAdder class which was added in java 1.8. I've tried to make it optional but failed (https://stackoverflow.com/questions/45045314/clojure-optional-definitions)
* I've modified the defcache macro (all tests pass) so I can actually override definitions

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

10 Answers

0 votes
by

Comment made by: seancorfield

It's a very interesting idea, thank you! I can definitely see value in this for a lot of users of {{core.cache}} (including myself).

I'll take a look over the patch but, in the meantime, I don't see your name listed on the Contributors' page -- so could you go through the process here https://clojure.org/community/contributors if you haven't already?

0 votes
by

Comment made by: akhomchenko

I have already signed the SLA with my email and github username. Maybe I did it wrong, I'll check again.

0 votes
by

Comment made by: seancorfield

Thanks. I'll confirm with Alex tomorrow since the website lags behind.

0 votes
by

Comment made by: akhomchenko

Good day.

Still no update on that contributors page. Could I help somehow to get it resolved?

Thank you.

0 votes
by

Comment made by: seancorfield

I asked Alex to check the recent CAs submitted -- I expect he'll be able to confirm next week. It'll take me a while to review and analyze the patch anyway (and I'm neck-deep in production roll-outs right now so be patient).

0 votes
by

Comment made by: akhomchenko

Ah, I've asked mostly for contributors page not displaying me issue :)

0 votes
by

Comment made by: akhomchenko

Has added some docs.

0 votes
by

Comment made by: akhomchenko

Good day. Any updates on this? Anything I can do to help? Thanks

0 votes
by

Comment made by: seancorfield

Still under consideration. I'm looking at a bunch of issues with this library and will include this as part of that with when I've figured out the other hard issues.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CCACHE-51 (reported by akhomchenko)
...