core.cache

SoftCaches update in-place, other cache types don't

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

I've successfully used SoftCaches in my project, and now I had the need for another simple cache, so I've created a BasicCache. Thereby, I fell into the trap that while SoftCaches update in-place, BasicCaches don't (and looking at the code, all other cache types except for aforementioned SoftCache don't, too).

user> (def C (cache/soft-cache-factory {}))
#'user/C
user> (cache/miss C :a 1)
{:a #<SoftReference java.lang.ref.SoftReference@2bf31c53>}
user> C
{:a #<SoftReference java.lang.ref.SoftReference@2bf31c53>} ;; updated in-place
user> (def C (cache/basic-cache-factory {}))
#'user/C
user> (cache/miss C :a 1)
{:a 1}
user> C                                                    ;; nope, no update, still initial value
{}

So with every cache type except for SoftCaches, you have to use the return value of `miss` (and `evict`). But the documentation doesn't mention that at all. The typical has?/miss/hit snippets explaining the usage pattern also discard the value of `miss`.

IMHO, a cache is something mutable and should of course update in place. I mean, when you `(def C (some-cache...))`, are you really supposed to use `alter-var-root` or make the cache Var dynamic and use `set!`?

Activity

Hide
Ambrose Bonnaire-Sergeant added a comment -

Patch distinguishes between mutable and immutable caches, and classifies SoftCache and TTLCache as mutable.

Show
Ambrose Bonnaire-Sergeant added a comment - Patch distinguishes between mutable and immutable caches, and classifies SoftCache and TTLCache as mutable.
Hide
Alex Miller added a comment -

Seems like renaming defcache breaks any external users of defcache?

A quick search of github https://github.com/search?l=clojure&q=defcache&type=Code&utf8=%E2%9C%93 shows many examples.

Show
Alex Miller added a comment - Seems like renaming defcache breaks any external users of defcache? A quick search of github https://github.com/search?l=clojure&q=defcache&type=Code&utf8=%E2%9C%93 shows many examples.
Hide
Sean Corfield added a comment -

Whilst I agree that SoftCache is an anomaly, I'd note that TTLCache is immutable (maybe it was mutable back in 2014?) and that SoftCache, by its very nature, is built on top of mutable Java data structures – so it's always going to be the odd one out. I consider this to be a documentation issue – happy to see issues opened suggesting improvements to the README or the docs tree (or patches!).

Show
Sean Corfield added a comment - Whilst I agree that SoftCache is an anomaly, I'd note that TTLCache is immutable (maybe it was mutable back in 2014?) and that SoftCache, by its very nature, is built on top of mutable Java data structures – so it's always going to be the odd one out. I consider this to be a documentation issue – happy to see issues opened suggesting improvements to the README or the docs tree (or patches!).
Hide
Sean Corfield added a comment -

Release 0.7.0

Show
Sean Corfield added a comment - Release 0.7.0

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: