Skip to content

Commit

Permalink
Fixed seeded lru map
Browse files Browse the repository at this point in the history
  • Loading branch information
fogus committed Mar 13, 2012
1 parent 582f86b commit d43678d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/main/clojure/clojure/core/cache.clj
Expand Up @@ -72,7 +72,7 @@
(empty [this#]
(seed this# (empty ~base-field)))
(equiv [_# other#]
(.equiv other# ~base-field))
(.equiv ~base-field other#))

clojure.lang.Seqable
(seq [_#]
Expand Down Expand Up @@ -193,9 +193,9 @@
limit)))
(miss [_ item result]
(let [tick+ (inc tick)]
(if-let [ks (keys lru)]
(let [k (apply min-key lru ks)]
(LRUCache. (-> cache (dissoc k) (assoc item result)) ;; expulsion case
(if (>= (count cache) limit)
(let [k (apply min-key lru (keys lru))]
(LRUCache. (-> cache (dissoc k) (assoc item result)) ;; eviction case
(-> lru (dissoc k) (assoc item tick+))
tick+
limit))
Expand All @@ -209,11 +209,11 @@
this
(LRUCache. (dissoc cache key)
(dissoc lru key)
tick
(inc tick)
limit))))
(seed [_ base]
(LRUCache. base
(into {} (for [x (range (- limit) 0)] [x x]))
(into {} (for [k (keys base)] [k 0]))
0
limit))
Object
Expand Down
11 changes: 9 additions & 2 deletions src/test/clojure/clojure/core/cache/tests.clj
Expand Up @@ -132,10 +132,17 @@
(do-dissoc (LRUCache. {:a 1 :b 2} {} 0 2))))

(deftest test-lru-cache
(testing "LRU-ness"
(testing "LRU-ness with empty cache"
(let [C (lru-cache-factory {} :limit 2)]
(are [x y] (= x y)
{:a 1, :b 2} (-> C (assoc :a 1) (assoc :b 2) .cache)))))
{:a 1, :b 2} (-> C (assoc :a 1) (assoc :b 2) .cache)
{:b 2, :c 3} (-> C (assoc :a 1) (assoc :b 2) (assoc :c 3) .cache)
{:a 1, :c 3} (-> C (assoc :a 1) (assoc :b 2) (.hit :a) (assoc :c 3) .cache))))
(testing "LRU-ness with seeded cache"
(let [C (lru-cache-factory {:a 1, :b 2} :limit 4)]
(are [x y] (= x y)
{:a 1, :b 2, :c 3, :d 4} (-> C (assoc :c 3) (assoc :d 4) .cache)
{:a 1, :c 3, :d 4, :e 5} (-> C (assoc :c 3) (assoc :d 4) (.hit :a) (assoc :e 5) .cache)))))

(deftest test-ttl-cache-ilookup
(testing "that the TTLCache can lookup via keywords"
Expand Down

0 comments on commit d43678d

Please sign in to comment.