Permalink
Browse files

Fixed seeded lru map

  • Loading branch information...
1 parent 582f86b commit d43678dd0dfcc6afe222fc05869902f363bc4844 @fogus fogus committed Mar 13, 2012
Showing with 15 additions and 8 deletions.
  1. +6 −6 src/main/clojure/clojure/core/cache.clj
  2. +9 −2 src/test/clojure/clojure/core/cache/tests.clj
@@ -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 [_#]
@@ -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))
@@ -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
@@ -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"

0 comments on commit d43678d

Please sign in to comment.