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.
With the help of Slack, I have a reproduction here:
(let [thread-count 20
cache-atom (-> {}
(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 "starting thread" i)
(.start (Thread. (fn []
(cache-wrapped/lookup-or-miss cache-atom "my-key"
(fn [k]
(swap! invocations-counter inc)
(Thread/sleep 3000)
"some value"))
(.countDown latch)))))
(.await latch)
(deref invocations-counter))
I would expect that invocations counter would be 1
but it's 20
(once per thread).