<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged core.memoize</title>
<link>https://ask.clojure.org/index.php/tag/core.memoize</link>
<description></description>
<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 '[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'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>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'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>Rogue backtick in core.memoize's Including.md impairs formatting</title>
<link>https://ask.clojure.org/index.php/10824/rogue-backtick-core-memoizes-including-impairs-formatting</link>
<description>&lt;p&gt;core.memoize's &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.memoize/blob/master/docs/Including.md#leiningen&quot;&gt;docs/Including.md&lt;/a&gt; contains a rogue &quot;`&quot; that messes up presentation a bit. I instinctively went to create a PR, but then was reminded that patches go in Jira. A bit of hoop-jumping later, and here is my suggested fix, having signed the CLA:&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/core.memoize/commit/56305f04e9b5a351bf760df663e0884013b43210&quot;&gt;https://github.com/clojure/core.memoize/commit/56305f04e9b5a351bf760df663e0884013b43210&lt;/a&gt;&lt;/p&gt;
</description>
<category>core.memoize</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10824/rogue-backtick-core-memoizes-including-impairs-formatting</guid>
<pubDate>Fri, 16 Jul 2021 16:34:18 +0000</pubDate>
</item>
</channel>
</rss>