根据使用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 线程数)]
(println "开始线程" i)
(.start(Thread.(fn []
(memoized-function "my-key")
(.countDown 锁存器))))
(.await 锁存器)
(assert (= 1 (deref 调用次数计数器)))