core.cache

Stats for cache

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code and Test

Description

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:

(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

Activity

Hide
Sean Corfield added a comment -

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?

Show
Sean Corfield added a comment - 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?
Hide
Oleksii Khomchenko added a comment - - edited

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

Show
Oleksii Khomchenko added a comment - - edited I have already signed the SLA with my email and github username. Maybe I did it wrong, I'll check again.
Hide
Sean Corfield added a comment -

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

Show
Sean Corfield added a comment - Thanks. I'll confirm with Alex tomorrow since the website lags behind.
Hide
Oleksii Khomchenko added a comment -

Good day.

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

Thank you.

Show
Oleksii Khomchenko added a comment - Good day. Still no update on that contributors page. Could I help somehow to get it resolved? Thank you.
Hide
Sean Corfield added a comment -

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).

Show
Sean Corfield added a comment - 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).
Hide
Oleksii Khomchenko added a comment -

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

Show
Oleksii Khomchenko added a comment - Ah, I've asked mostly for contributors page not displaying me issue
Hide
Oleksii Khomchenko added a comment -

Has added some docs.

Show
Oleksii Khomchenko added a comment - Has added some docs.
Hide
Oleksii Khomchenko added a comment -

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

Show
Oleksii Khomchenko added a comment - Good day. Any updates on this? Anything I can do to help? Thanks
Hide
Sean Corfield added a comment -

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.

Show
Sean Corfield added a comment - 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.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: