<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions in core.cache</title>
<link>https://ask.clojure.org/index.php/questions/contrib-libs/core-cache</link>
<description>Powered by Question2Answer</description>
<item>
<title>LRUCache works as FIFOCache when composed with a TTLCache</title>
<link>https://ask.clojure.org/index.php/15233/lrucache-works-as-fifocache-when-composed-with-a-ttlcache</link>
<description>&lt;p&gt;When composing caches, hits are not propagated to inner caching layers. This makes, for example, a LRUCache work as a FIFOCache as it doesn&#039;t update its information about usage recency.&lt;/p&gt;
&lt;p&gt;This can be seen in the REPL reproduction below. &lt;code&gt;:b&lt;/code&gt; is evicted even though it had been accessed 3 times.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (require &#039;[clojure.core.cache :as cache])
nil
user=&amp;gt; (def c-cache (-&amp;gt; {:a 10 :b 20} 
                        (cache/lru-cache-factory :threshold 3) 
                        (cache/ttl-cache-factory :ttl 30000)))
#&#039;user/c-cache
user=&amp;gt; (def c-cache2 (cache/miss c-cache :c 30))
#&#039;user/c-cache2
user=&amp;gt; (cache/lookup c-cache2 :b)
20
user=&amp;gt; (cache/lookup c-cache2 :b)
20
user=&amp;gt; (cache/lookup c-cache2 :b)
20
user=&amp;gt; (cache/miss c-cache2 :d 40)
{:a 10, :c 30, :d 40}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, either &lt;code&gt;:a&lt;/code&gt; or &lt;code&gt;:c&lt;/code&gt; should have been evicted.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15233/lrucache-works-as-fifocache-when-composed-with-a-ttlcache</guid>
<pubDate>Wed, 09 Sep 2026 14:19:55 +0000</pubDate>
</item>
<item>
<title>clojure.core.cache caches delays that hold exceptions as of version 1.2.254</title>
<link>https://ask.clojure.org/index.php/14965/clojure-core-cache-caches-delays-that-hold-exceptions-version</link>
<description>&lt;p&gt;As of version 1.2.254 of &lt;code&gt;org.clojure/core.cache&lt;/code&gt;, using a &lt;code&gt;value-fn&lt;/code&gt; that throws will cause an entry to be added to the cache associated with a Delay with &lt;code&gt;:status :failed&lt;/code&gt; and the exception as a value.&lt;/p&gt;
&lt;p&gt;The code below can be used to compare the behavior between versions 1.1.234 and 1.2.254.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require &#039;[clojure.core.cache.wrapped :as cw])

(def counter (atom 0))

(defn value-fn [v]
  (prn :value-fn-called)
  (swap! counter inc)
  (if (= @counter 1)
    (throw (ex-info &quot;thrown&quot; {}))
    v))

(def test-cache (cw/lu-cache-factory {} :threshold 10))

(reset! counter 0)
; this will throw
(cw/lookup-or-miss test-cache &quot;throw&quot; value-fn)
; in 1.1.234 this will call value-fn again and not throw; in 1.2.254, it won&#039;t call and return
; the cached delay result, which is an exception
(cw/lookup-or-miss test-cache &quot;throw&quot; value-fn)

; evict to force call to value-fn
(cw/evict test-cache &quot;throw&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is related to work done in &lt;a rel=&quot;nofollow&quot; href=&quot;http://clojure.atlassian.net/browse/CCACHE-65&quot;&gt;CCACHE-65&lt;/a&gt; to avoid cache stampede in multi-threaded applications, and could be related to what has been reported in &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CMEMOIZE-31&quot;&gt;CMEMOIZE-31&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This was initially reported &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C03S1KBA2/p1771873303347759&quot;&gt;in Clojurians Slack&lt;/a&gt;.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14965/clojure-core-cache-caches-delays-that-hold-exceptions-version</guid>
<pubDate>Mon, 23 Feb 2026 19:43:34 +0000</pubDate>
</item>
<item>
<title>Core Cache and Count</title>
<link>https://ask.clojure.org/index.php/14910/core-cache-and-count</link>
<description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I notice that in the wrapped namespace of core.cache, there is a &lt;strong&gt;has?&lt;/strong&gt; function that checks the wrapped cache if it has an element or not. It does this by delegating to the underlying cache &lt;strong&gt;c/has?&lt;/strong&gt; function of the wrapped cache.&lt;/p&gt;
&lt;p&gt;For reporting purposes, I need to get the current size of the cache (with the caveat of course that the size could be approximate, given the particular nature of a particular cache - but approximation is fine).&lt;/p&gt;
&lt;p&gt;Unfortuantely, a &lt;strong&gt;size&lt;/strong&gt; or &lt;strong&gt;count&lt;/strong&gt; function doesn&#039;t exist in the wrapped namespace, it only exists on the wrapped cache itself - thus doing:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(count @wrapped-cache)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; does return the value. &lt;/p&gt;
&lt;p&gt;It would be good, however,  if there was also a similar function in the wrapped namespace, something like this (using &lt;strong&gt;size&lt;/strong&gt; as the function name, but could be something else):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn size
   [cache-atom]
   (c/count @cache-atom)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rationale being that having this function exposed in the wrapped namespace keeps the code consistent - i.e., whilst I can continue to do:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(count @wrapped-cache)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It would more preferrable to keep using the wrapped namespace (as I do with other operations, such as evict), i.e., something along the lines of:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(wrapped/size wrapped-cache)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do you think this suggestion might be something that could be included in the wrapped namespace?&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;
&lt;p&gt;-=david=-&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14910/core-cache-and-count</guid>
<pubDate>Wed, 28 Jan 2026 16:54:22 +0000</pubDate>
</item>
<item>
<title>Is the clojure.cache behavior for lookup-or-miss value-fn re-entrancy broken or am I misunderstanding the docstring?</title>
<link>https://ask.clojure.org/index.php/14037/clojure-behavior-lookup-entrancy-misunderstanding-docstring</link>
<description>&lt;p&gt;I’m trying to reconcile clojure.cache behavior between what the docstring/code both suggest, and what I observe actually happening.  I am referring to this line:&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.cache/blob/99fbd669a429bc800ff32b64b21cf82b6b486f06/src/main/clojure/clojure/core/cache/wrapped.clj#L43&quot;&gt;https://github.com/clojure/core.cache/blob/99fbd669a429bc800ff32b64b21cf82b6b486f06/src/main/clojure/clojure/core/cache/wrapped.clj#L43&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;value-fn (and wrap-fn) will only be called (at most) once even in the &lt;br&gt;
case of retries, so there is no risk of cache stampede.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It seems quite trivial to get the value-fn to be called more than once when the cache is under contention.  I’ve emulated this behavior in the repl, as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require &#039;[clojure.core.cache.wrapped :as cache])
=&amp;gt; nil
(def c (cache/fifo-cache-factory {}))
=&amp;gt; #&#039;user/c
(pmap (fn [_] (cache/lookup-or-miss c :foo (fn [k] (println &quot;miss&quot;) k))) (range 10))
miss
miss
miss
miss
miss
miss
miss
miss
=&amp;gt; (:foo :foo :foo :foo :foo :foo :foo :foo :foo :foo)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case, the miss function was invoked 8 out of 10 times when threads were competing.  I see this same behavior in the real world, too.&lt;/p&gt;
&lt;p&gt;What I don&#039;t fully understand is why when the swap! protects the call to c/through-cache. I&#039;m guessing it has to do with how swap resolves conflicts. But if so, doesn&#039;t that mean the docstring is wrong?&lt;/p&gt;
&lt;p&gt;This is reproducible for me in both fifo and ttl caches.  I haven&#039;t tried the others.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14037/clojure-behavior-lookup-entrancy-misunderstanding-docstring</guid>
<pubDate>Mon, 05 Aug 2024 17:56:40 +0000</pubDate>
</item>
<item>
<title>clojure.core.cache/TTLCache not extending TTL on hit?</title>
<link>https://ask.clojure.org/index.php/13584/clojure-core-cache-ttlcache-not-extending-ttl-on-hit</link>
<description>&lt;p&gt;It seems that the TTL cache should reset an item&#039;s TTL on a hit, similar&lt;br&gt;
to how a &lt;code&gt;hit&lt;/code&gt; on the LRU cache increases the recency of the item, however upon checking the code it is effectively a no-op. Is this intentional?&lt;/p&gt;
&lt;p&gt;I have &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/rschmukler/core.cache/commit/10015e7ddf528b3dae72ba9881cdf00a4edc62a5&quot;&gt;a branch with a fix&lt;/a&gt; that you are welcome to use if it isn&#039;t intentional, or I can go through the process of formally submitting a patch.&lt;/p&gt;
&lt;p&gt;If it is intentional, should the hit behavior for all caches be no-ops? ie. it is especially surprising that a &lt;code&gt;hit&lt;/code&gt; sometimes extends the life of an item (LRU cache) and sometimes doesn&#039;t (TTL cache)&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13584/clojure-core-cache-ttlcache-not-extending-ttl-on-hit</guid>
<pubDate>Tue, 02 Jan 2024 17:11:21 +0000</pubDate>
</item>
<item>
<title>Suggest using (fn [&amp; args] x) over constantly in README.md</title>
<link>https://ask.clojure.org/index.php/12781/suggest-using-fn-args-x-over-constantly-in-readme-md</link>
<description>&lt;p&gt;Was misled by the example in the README.md the other day. &lt;/p&gt;
&lt;p&gt;I did the equivalent of the following&lt;br&gt;
&lt;code&gt;(w/lookup-or-miss C3 :b (constantly (do (Thread/sleep 1000) 42)))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;instead of&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(w/lookup-or-miss C3 :b (fn [&amp;amp; args] (do (Thread/sleep 1000) 42)))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Resulting in the IO call I was doing which takes the place of &lt;code&gt;(Thread/sleep 1000)&lt;/code&gt; to always be evaluated. This resulted in a &quot;cache&quot; that returns the value from the cache but will always trigger the call&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12781/suggest-using-fn-args-x-over-constantly-in-readme-md</guid>
<pubDate>Sat, 18 Mar 2023 02:10:19 +0000</pubDate>
</item>
<item>
<title>Could some calls to clojure.core/= be replaced by clojure.core/identical?</title>
<link>https://ask.clojure.org/index.php/12721/could-some-calls-clojure-core-replaced-clojure-core-identical</link>
<description>&lt;p&gt;I&#039;m using &lt;code&gt;core.cache&lt;/code&gt; in combination with &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/raxod502/lazy-map&quot;&gt;&lt;code&gt;LazyMap&lt;/code&gt;&lt;/a&gt;. Lazy maps are map-like objects (new data type encapsulating maps and adding some logic to auto-force delayed values associated with keys).&lt;/p&gt;
&lt;p&gt;When I started putting lazy maps as values of FIFO Cache I observed that all values are being realized just after the map lands in cache. After some digging I&#039;ve found that it is caused by expression &lt;code&gt;(= ::expired v)&lt;/code&gt; in &lt;code&gt;clojure.core.cache.wrapped/lookup-or-miss&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;What happens is that &lt;code&gt;=&lt;/code&gt; internally calls &lt;code&gt;clojure.lang.Util/equiv&lt;/code&gt; (since &lt;code&gt;LazyMap&lt;/code&gt; implements &lt;code&gt;IPersistentCollection&lt;/code&gt;) and then, after some dispatching, &lt;code&gt;equiv&lt;/code&gt; is called to check whether the contents of a lazy map equals to other object (implicitly other map). This is the desired behavior and lazy map realizes all values to make comparison possible.&lt;/p&gt;
&lt;p&gt;However, when comparing a map-like object to something which is not a map (nor a collection), short-circuit would be welcome as soon as we know that it will for sure return &lt;code&gt;false&lt;/code&gt;. But changing that is probably more related to Clojure itself and things like &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-1375&quot;&gt;CLJ-1375&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What could be done on &lt;code&gt;core.cache&lt;/code&gt;&#039;s side, if that&#039;s not too problematic, would be a replacement of&lt;/p&gt;
&lt;p&gt; &lt;code&gt;(= ::expired v)&lt;/code&gt; with &lt;code&gt;(identical? ::expired v)&lt;/code&gt;   &lt;/p&gt;
&lt;p&gt;in &lt;code&gt;clojure.core.cache.wrapped&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It would not only fix corner cases like mine but also speed things up a bit (explicit referential equality rocks when it comes to Clojure keywords).&lt;/p&gt;
&lt;p&gt;Other expressions where that could be changed (just in case) are:&lt;br&gt;
 &lt;code&gt;(= ::nope ret)&lt;/code&gt; and &lt;code&gt;(= ::nil v)&lt;/code&gt; from &lt;code&gt;clojure.core.cache&lt;/code&gt;.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12721/could-some-calls-clojure-core-replaced-clojure-core-identical</guid>
<pubDate>Thu, 02 Mar 2023 22:47:18 +0000</pubDate>
</item>
<item>
<title>Multi-threaded cache stampede in core.cache</title>
<link>https://ask.clojure.org/index.php/12567/multi-threaded-cache-stampede-in-core-cache</link>
<description>&lt;p&gt;We recently deployed core.cache in a web context, and we found out that there was a cache stampede for multi-threaded access. It was obvious in retrospect, but the references to stampede in the docs made me think that this was handled.&lt;/p&gt;
&lt;p&gt;With the help of &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C03S1KBA2/p1674040460264619&quot;&gt;Slack&lt;/a&gt;, I have a reproduction here:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [thread-count 20
      cache-atom (-&amp;gt; {}
                   (cache/ttl-cache-factory :ttl 120000)
                   (cache/lu-cache-factory :threshold 100)
                   (atom))
      latch (java.util.concurrent.CountDownLatch. thread-count)
      invocations-counter (atom 0)]
 (doseq [i (range thread-count)]
   (println &quot;starting thread&quot; i)
   (.start (Thread. (fn []
                     (cache-wrapped/lookup-or-miss cache-atom &quot;my-key&quot;
                                                   (fn [k]
                                                     (swap! invocations-counter inc)
                                                     (Thread/sleep 3000)
                                                     &quot;some value&quot;))
                     (.countDown latch)))))

 (.await latch)
 (deref invocations-counter))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I would expect that invocations counter would be &lt;code&gt;1&lt;/code&gt; but it&#039;s &lt;code&gt;20&lt;/code&gt; (once per thread).&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12567/multi-threaded-cache-stampede-in-core-cache</guid>
<pubDate>Wed, 18 Jan 2023 18:06:18 +0000</pubDate>
</item>
<item>
<title>Docs for core.cache.documentation are ambigous</title>
<link>https://ask.clojure.org/index.php/12517/docs-for-core-cache-documentation-are-ambigous</link>
<description>&lt;p&gt;The doc example on &lt;a rel=&quot;nofollow&quot; href=&quot;https://cljdoc.org/d/org.clojure/core.cache/1.0.225/doc/using-core-cache&quot;&gt;https://cljdoc.org/d/org.clojure/core.cache/1.0.225/doc/using-core-cache&lt;/a&gt; does not mention that one has to choose between &lt;strong&gt;core.cache&lt;/strong&gt; and &lt;strong&gt;core.cache.wrapped&lt;/strong&gt; More specifically, when creating the cache, the example has imported the core.cache namespace. However later on, when coming to lookup-or-miss, &lt;strong&gt;core.cache.wrapped&lt;/strong&gt; is assumed to be imported. When one tries to use &lt;strong&gt;wrapped/lookup-or-miss&lt;/strong&gt; with a cache created from the factory in &lt;strong&gt;core.cache&lt;/strong&gt;, one encounters the rather ambigous expection:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class clojure.core.cache.LRUCache cannot be cast to class clojure.lang.IAtom
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I think providing more complete examples in the docs would aleviate this issue.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12517/docs-for-core-cache-documentation-are-ambigous</guid>
<pubDate>Thu, 29 Dec 2022 19:06:56 +0000</pubDate>
</item>
<item>
<title>Could there be a ClojureScript version of core.cache?</title>
<link>https://ask.clojure.org/index.php/12143/could-there-be-a-clojurescript-version-of-core-cache</link>
<description>&lt;p&gt;CCACHE-42 &quot;Porting core.cache to ClojureScript&quot; was declined for infeasibility in 2018, but the JavaScript situation has improved since then.  WeakRef has spread to all MDN-tracked environments except Opera.&lt;/p&gt;
&lt;p&gt;Lack of a ClojureScript port of data.priority-map (used by 2 cache types) is another challenge noted by CCACHE-42.  An issue for a cljc port of data.priority-map is currently open: DPRIMAP-8.&lt;/p&gt;
&lt;p&gt;As more data processing moves into browsers and Node.js, core.cache is one of the rare Clojure gems that is still pinned to the JVM.  &lt;/p&gt;
&lt;p&gt;Since a cljc port of core.cache is not as infeasible as it used to be (and to formalize an interest in DPRIMAP-8), may we reopen CCACHE-42?&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12143/could-there-be-a-clojurescript-version-of-core-cache</guid>
<pubDate>Sun, 04 Sep 2022 11:54:10 +0000</pubDate>
</item>
<item>
<title>Issues with LRU and LU cache in core.cache</title>
<link>https://ask.clojure.org/index.php/10849/issues-with-lru-and-lu-cache-in-core-cache</link>
<description>&lt;p&gt;I am using core.cache and was wondering why the creation of lru cache with large threshold and no intial data was taking so long.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require &#039;[clojure.core.cache.wrapped :as cw])
(time (cw/lu-cache-factory {} :threshold 10000000))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bottleneck seems to be this &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.cache/blob/master/src/main/clojure/clojure/core/cache.clj#L210&quot;&gt;line&lt;/a&gt; where the lru list gets filled with dummy values. I looked at the LRUCache code and don&#039;t see any reason for the lru list to not grow organically and only initialize it with the base. I also tested the LRUCache with the simpler initialization function &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn- build-leastness-queue
  [base start-at]
  (into (clojure.data.priority-map/priority-map) (for [[k _] base] [k start-at])))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and all LRUCache tests still pass.&lt;/p&gt;
&lt;p&gt;The original &lt;code&gt;build-leastness-queue&lt;/code&gt; is also used for the LUCache and here the tests no longer pass when using the new version. While trying to also make the modified version work for the LUCache I realized that it is currently kind of broken.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require &#039;[clojure.core.cache :as c])
(def ca (c/lu-cache-factory {:a 1 :b 2} :threshold 2))
 
(-&amp;gt; ca
    (assoc :c 3)
    .lu)
;; =&amp;gt; {:a 0, :c 1}

(-&amp;gt; ca
    (dissoc :a)
    (assoc :c 3)
   .lu)
;; =&amp;gt; {:c 0, :b 0}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So with the current implementation the initial LU priority of newly assoced (missed) item depends on whether the lu cache is currently full or not.&lt;/p&gt;
&lt;p&gt;I signed the Clojure CA this morning and was wondering if somebody could add me to the JIRA so I can propose a patch for these issues (name is FiVo over there).&lt;/p&gt;
&lt;p&gt;One more question concerning the semantics of the LUCache. The tests contain the comment &lt;em&gt;ensure that the test result does not rely on arbitrary tie-breakers in number of hits&lt;/em&gt;, but it seems it relies on arbitrary tie-breakes:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def ca (c/lu-cache-factory {:a 1} :threshold 2))

(-&amp;gt; ca
    (assoc :b 2)
    (assoc :c 3)
    (assoc :d 4))
 ;; =&amp;gt; {:b 2, :d 4} 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Should later assoced items have a higher priority? This would make the patch a lot more complicated and I strongly favor keeping arbitrary tie-breaks.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10849/issues-with-lru-and-lu-cache-in-core-cache</guid>
<pubDate>Mon, 26 Jul 2021 11:13:13 +0000</pubDate>
</item>
<item>
<title>Why do core.cache&#039;s deftypes not implement equality?</title>
<link>https://ask.clojure.org/index.php/10370/why-do-core-caches-deftypes-not-implement-equality</link>
<description>&lt;p&gt;I&#039;m using &lt;code&gt;core.cache&lt;/code&gt;, in particular the &lt;code&gt;clojure.core.cache.wrapped&lt;/code&gt; ns.&lt;/p&gt;
&lt;p&gt;I found out that even after dereferencing one of the atoms provided by the namespace&#039;s factories, the obtained map will not &lt;code&gt;=&lt;/code&gt; an equivalent-looking map:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;(= {1 1} @(clojure.core.cache.wrapped/lru-cache-factory {1 1})) ;; =&amp;gt; false&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The reason appears to be that &lt;code&gt;defcache LRUCache&lt;/code&gt; (which boils down to a &lt;code&gt;deftype&lt;/code&gt;) does not implement equality.&lt;/p&gt;
&lt;p&gt;Could this be fixed?&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10370/why-do-core-caches-deftypes-not-implement-equality</guid>
<pubDate>Thu, 25 Mar 2021 19:01:05 +0000</pubDate>
</item>
<item>
<title>core.cache: TTLCacheQ timer is not monotonic</title>
<link>https://ask.clojure.org/index.php/10306/core-cache-ttlcacheq-timer-is-not-monotonic</link>
<description>&lt;p&gt;TTLCacheQ is built on top of &lt;code&gt;System/currentTimeMillis&lt;/code&gt; which is not monotonic, the behavior of the cache could be affected by clock adjustments (ex NTP). My suggestion was to change it to &lt;code&gt;System/nanoTime&lt;/code&gt; which is monotonic and therefore more consistent to calculate the elapsed time.&lt;/p&gt;
&lt;p&gt;If this clock adjustments effect is desired on the ttl cache maybe it should become another cache strategy with a doc on this behavior.&lt;/p&gt;
&lt;p&gt;PS: I&#039;ve created a .patch to fix this, signed the CA but I don&#039;t know where to ask for a JIRA account to open an issue.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10306/core-cache-ttlcacheq-timer-is-not-monotonic</guid>
<pubDate>Mon, 08 Mar 2021 14:29:46 +0000</pubDate>
</item>
<item>
<title>Split protocols from implementations</title>
<link>https://ask.clojure.org/index.php/673/split-protocols-from-implementations</link>
<description>&lt;p&gt;Hi Sean. I would like to propose a change mainly for starting being ClojureScript friendly.&lt;/p&gt;
&lt;p&gt;It would be very useful to have all the protocols in one {{.cljc}} file and the implementations in different ones. This would open the doors for a ClojureScript port of some of the implementations down the road.&lt;/p&gt;
&lt;p&gt;I don&#039;t know exactly how the split could be organized as I have just glanced the code but I am opening this issue to taste the waters about the idea.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/673/split-protocols-from-implementations</guid>
<pubDate>Tue, 19 Mar 2019 22:19:05 +0000</pubDate>
</item>
<item>
<title>Stats for cache</title>
<link>https://ask.clojure.org/index.php/668/stats-for-cache</link>
<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.&lt;br /&gt;
&lt;br /&gt;
Quick demo:&lt;br /&gt;
&lt;br /&gt;
{code:none}&lt;br /&gt;
(require &amp;#039;[clojure.core.cache :as core.cache]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#039;[clojure.core.cache.stats :as ccs]&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#039;[clojure.core.cache.stats.counters :as ccs.counters])&lt;br /&gt;
&lt;br /&gt;
(def cache (-&amp;gt; {} core.cache/lru-cache-factory ccs/measured-cache))&lt;br /&gt;
(ccs/stats cache) &amp;nbsp;; {:hit 0, :miss 0, :request 0, :hit-ratio 1.0, :miss-ratio 1.0}&lt;br /&gt;
(def cache (assoc cache :foo &amp;quot;bar&amp;quot;))&lt;br /&gt;
(ccs/stats cache) &amp;nbsp;; {:hit 0, :miss 1, :request 1, :hit-ratio 0.0, :miss-ratio 1.0}&lt;br /&gt;
(get cache :foo) &amp;nbsp;; &amp;quot;bar&amp;quot;&lt;br /&gt;
(get cache :foo) &amp;nbsp;; &amp;quot;bar&amp;quot;&lt;br /&gt;
(ccs/stats cache) &amp;nbsp;; {:hit 2, :miss 1, :request 3, :hit-ratio 0.6666666666666666, :miss-ratio 0.3333333333333333}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What&amp;#039;s new:&lt;br /&gt;
&lt;br /&gt;
* 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&lt;br /&gt;
* 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&lt;br /&gt;
* tests are there for all the methods that could be invoked&lt;br /&gt;
&lt;br /&gt;
Caveats (in no particular order):&lt;br /&gt;
&lt;br /&gt;
* LongAdderStatsCounter introduces hard dependency on LongAdder class which was added in java 1.8. I&amp;#039;ve tried to make it optional but failed (&lt;a href=&quot;https://stackoverflow.com/questions/45045314/clojure-optional-definitions&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;https://stackoverflow.com/questions/45045314/clojure-optional-definitions&lt;/a&gt;)&lt;br /&gt;
* I&amp;#039;ve modified the defcache macro (all tests pass) so I can actually override definitions&lt;br /&gt;
&lt;br /&gt;
Things to finish before merge:&lt;br /&gt;
&lt;br /&gt;
* if you would like the idea I&amp;#039;ll add more docs (maybe I should have started with this one so higher chances to be accepted?)&lt;br /&gt;
* polish code (I&amp;#039;m not an expert in Clojure at all), naming and namespaces&lt;br /&gt;
&lt;br /&gt;
Thank you in advance for the feedback</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/668/stats-for-cache</guid>
<pubDate>Wed, 03 Oct 2018 09:04:21 +0000</pubDate>
</item>
<item>
<title>Suggested usage subject to Cache Stampede</title>
<link>https://ask.clojure.org/index.php/674/suggested-usage-subject-to-cache-stampede</link>
<description>&lt;p&gt;I&#039;ve been trying to wrap my head around core.cache, and it seems to me that the suggest use in the documentation, i.e.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
(defn get-data [key]&lt;br&gt;
  (cache/lookup (swap! cache-store&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;                   #(if (cache/has? % key)
                      (cache/hit % key)
                      (cache/miss % key (retrieve-data key))))
            key))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;will result in a (link: &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Cache_stampede&quot;&gt;https://en.wikipedia.org/wiki/Cache_stampede&lt;/a&gt; text: Cache Stampede) since {{(retrieve-data key)}} might be called multiple times if &lt;code&gt;swap!&lt;/code&gt; retries due to compare-and-swap misses. Unfortunately I don&#039;t have a suggestion on how this could be fixed.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/674/suggested-usage-subject-to-cache-stampede</guid>
<pubDate>Sat, 22 Sep 2018 17:10:35 +0000</pubDate>
</item>
<item>
<title>Add ARC or CAR algorithm</title>
<link>https://ask.clojure.org/index.php/666/add-arc-or-car-algorithm</link>
<description>&lt;p&gt;Both ARC and CAR algorithm&#039;s look like they could be an improvement on LIRS and LRU algorithms. Specifically, both of them are better at keeping frequently used items in cache.&lt;/p&gt;
&lt;p&gt;From: &lt;a rel=&quot;nofollow&quot; href=&quot;http://en.wikipedia.org/wiki/Cache_algorithms#Examples&quot;&gt;http://en.wikipedia.org/wiki/Cache_algorithms#Examples&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Adaptive Replacement Cache (ARC)&lt;br&gt;
Constantly balances between LRU and LFU, to improve the combined result. ARC improves on SLRU by using information about recently-evicted cache items to dynamically adjust the size of the protected segment and the probationary segment to make the best use of the available cache space.&lt;/p&gt;
&lt;p&gt;Clock with Adaptive Replacement (CAR)&lt;br&gt;
Combines Adaptive Replacement Cache (ARC) and CLOCK. CAR has performance comparable to ARC, and substantially outperforms both LRU and CLOCK. Like ARC, CAR is self-tuning and requires no user-specified magic parameters.&lt;/p&gt;
&lt;p&gt;More details on ARC are at &lt;a rel=&quot;nofollow&quot; href=&quot;http://blog.acolyer.org/2014/10/08/outperforming-lru-with-an-adaptive-replacement-cache-algorithm/&quot;&gt;http://blog.acolyer.org/2014/10/08/outperforming-lru-with-an-adaptive-replacement-cache-algorithm/&lt;/a&gt;, and the paper is at &lt;a rel=&quot;nofollow&quot; href=&quot;http://dbs.uni-leipzig.de/file/ARC.pdf&quot;&gt;http://dbs.uni-leipzig.de/file/ARC.pdf&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;CAR details are at &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.usenix.org/conference/fast-04/car-clock-adaptive-replacement&quot;&gt;https://www.usenix.org/conference/fast-04/car-clock-adaptive-replacement&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If there is interest in this then I could look at benchmarking both of them, and putting together a patch.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/666/add-arc-or-car-algorithm</guid>
<pubDate>Wed, 26 Nov 2014 03:10:05 +0000</pubDate>
</item>
<item>
<title>Add weak-cache</title>
<link>https://ask.clojure.org/index.php/667/add-weak-cache</link>
<description>&lt;p&gt;Makes a weak-cache-factory available.  Entries will be eagerly collected by the jvm, rather than only when there&#039;s memory pressure.&lt;br&gt;
Implementation is fairly simple.  I changed SoftCache to NonStrongCache and added an extra record parameter, which is the function&lt;br&gt;
used to create the reference.&lt;br&gt;
See &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/pnf/core.cache/compare/clojure:master...master&quot;&gt;https://github.com/pnf/core.cache/compare/clojure:master...master&lt;/a&gt;&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/667/add-weak-cache</guid>
<pubDate>Sun, 24 Aug 2014 14:52:50 +0000</pubDate>
</item>
<item>
<title>LIRSCache defect allows it&#039;s memory use to grow without bound</title>
<link>https://ask.clojure.org/index.php/671/lirscache-defect-allows-its-memory-use-to-grow-without-bound</link>
<description>&lt;p&gt;Hello! I was messing around with the LIRSCache and ran into what I think is a bug.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.cache/blob/master/src/main/clojure/clojure/core/cache.clj#L371&quot;&gt;https://github.com/clojure/core.cache/blob/master/src/main/clojure/clojure/core/cache.clj#L371&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the case of a bunch of misses on unique keys, then the stack S will grow without bound and violate the limit that you give it.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(def C (cache/lirs-cache-factory {} :s-history-limit 2 :q-history-limit 1))
(defn populate [n] (let [pairs (map (fn [x] [x x]) (range n))] (reduce (fn [cache [k v]] (cache/miss cache k v)) C pairs)))
(.toString (populate 10))
&quot;{9 9, 1 1, 0 0}, {0 1, 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10}, {9 10}, 10, 2, 1&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;You can see that the S stack is growing without bound, and if you do (populate 1000), then it is 1000 pieces large.&lt;/p&gt;
&lt;p&gt;I&#039;m not sure what the desired outcome is, but any time we add something to one of the queues, we need to make sure that we&#039;re doing the right thing with what is kicked out, unless I&#039;m misinterpreting things.&lt;/p&gt;
&lt;p&gt;Thanks for making Clojure awesome!&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/671/lirscache-defect-allows-its-memory-use-to-grow-without-bound</guid>
<pubDate>Thu, 10 Oct 2013 21:51:57 +0000</pubDate>
</item>
<item>
<title>Create function backed cache</title>
<link>https://ask.clojure.org/index.php/670/create-function-backed-cache</link>
<description>&lt;p&gt;A cache implementation that is backed by a function that performs some action on a cache miss could serve as a front for any of the existing cache impls.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/670/create-function-backed-cache</guid>
<pubDate>Fri, 16 Dec 2011 14:57:38 +0000</pubDate>
</item>
<item>
<title>Benchmark v0.5.x against Google Guava</title>
<link>https://ask.clojure.org/index.php/672/benchmark-v0-5-x-against-google-guava</link>
<description>&lt;p&gt;Perform some tests and benchmarks of core.cache and Google Guava&#039;s com.google.common.cache library.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/672/benchmark-v0-5-x-against-google-guava</guid>
<pubDate>Fri, 16 Dec 2011 14:55:39 +0000</pubDate>
</item>
<item>
<title>Add eviction implementation to LIRSCache</title>
<link>https://ask.clojure.org/index.php/669/add-eviction-implementation-to-lirscache</link>
<description>&lt;p&gt;The {{evict}} method in the ProtocolCache needs implementation for {{LIRSCache}}.  I will start initially with a single key eviction method to start.  The {{evict}} method would form the basis for the associative {{dissoc}} which in turn forms the basis for proper limited seeding.  LIRS poses an additional complication due to its dual limit lists.  Currently only the {{BasicCache}} impl has {{evict}}.&lt;/p&gt;
</description>
<category>core.cache</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/669/add-eviction-implementation-to-lirscache</guid>
<pubDate>Thu, 08 Dec 2011 20:38:28 +0000</pubDate>
</item>
</channel>
</rss>