根据使用 core.memoize 的建议,它按预期工作
(let [thread-count 20
invocations-counter (atom 0)
expensive-function (fn [k]
(swap! invocations-counter inc)
(Thread/sleep 3000)
(str "value-" k))
cache (-> {}
(cache/ttl-cache-factory :ttl 120000)
(cache/lu-cache-factory :threshold 100))
memoized-function (memoize/memoizer expensive-function cache)
锁存器(java.util.concurrent.CountDownLatch. 线程数)]
(doseq [i (range 线程数)]
(打印 "开始线程" i)
(.start (Thread. (fn []
(memoized-function "my-key")
(.countDown 锁存器))))
(.await 锁存器)
(断言 (= 1 (deref 调用计数器)))