Skip to content

Commit 6fe6094

Browse files
committed
CCACHE-70: TTL cache update hit entry instead of noop.
1 parent bdf41c6 commit 6fe6094

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎src/main/clojure/clojure/core/cache.clj‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
The contract is that said cache should return an instance of its
4040
own type."))
4141

42+
(extend-protocol CacheProtocol
43+
clojure.lang.IPersistentMap
44+
(hit [this _] this))
45+
4246
(def ^{:private true} default-wrapper-fn #(%1 %2))
4347

4448
(defn through
@@ -281,7 +285,8 @@
281285
t)
282286
ttl-ms))
283287
(contains? cache item)))
284-
(hit [this item] this)
288+
(hit [this item]
289+
(TTLCacheQ. (hit cache item) ttl q gen ttl-ms))
285290
(miss [this item result]
286291
(let [now (System/currentTimeMillis)
287292
[kill-old q'] (key-killer-q ttl q ttl-ms now)]

‎src/test/clojure/clojure/core/cache_test.clj‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,15 @@
252252
(testing "TTL cache does not contain a value that was removed from underlying cache."
253253
(let [underlying-cache (lru-cache-factory {} :threshold 1)
254254
C (ttl-cache-factory underlying-cache :ttl 360000)]
255-
(is (not (-> C (assoc :a 1) (assoc :b 2) (has? :a)))))))
255+
(is (not (-> C (assoc :a 1) (assoc :b 2) (has? :a))))))
256+
(testing "TTL cache propagates hits to the underlying cache's recency tracking."
257+
(let [underlying-cache (lru-cache-factory {} :threshold 2)
258+
C (-> (ttl-cache-factory underlying-cache :ttl 360000)
259+
(assoc :a 1)
260+
(assoc :b 2))
261+
C' (-> C (hit :a) (hit :a) (assoc :c 3))]
262+
(is (has? C' :a)
263+
":a was hit twice and should not have been evicted in favor of :b"))))
256264

257265
(deftest test-lu-cache-ilookup
258266
(testing "that the LUCache can lookup via keywords"
@@ -529,3 +537,13 @@ N non-resident HIR block
529537
(let [c (fifo-cache-factory {:a 1 :b 2} :threshold 2)]
530538
(is (= #{:a :c} (set (-> c (evict :b) (miss :c 42) (.q)))))
531539
(is (= #{:c :d} (set (-> c (evict :b) (miss :c 42) (miss :d 43) (.q)))))))
540+
541+
(deftest propagation-to-composed-cache70
542+
(testing "hits on a composed cache (TTL wrapping LRU) propagate to the wrapped cache's recency tracking"
543+
(let [c-cache (-> {:a 10 :b 20}
544+
(lru-cache-factory :threshold 3)
545+
(ttl-cache-factory :ttl 30000))
546+
c-cache2 (miss c-cache :c 30)
547+
c-cache3 (-> c-cache2 (hit :b) (hit :b) (hit :b))
548+
c-cache4 (miss c-cache3 :d 40)]
549+
(is (has? c-cache4 :b) ":b was hit 3 times and should not have been evicted in favor of :a or :c"))))

0 commit comments

Comments
 (0)