Permalink
Browse files

LRU and LU caches adhere to their contracts

  • Loading branch information...
fogus committed Jul 23, 2012
1 parent 0f92a22 commit 0b17c52bac6c5cb007375566d631d6360bb1ec00
Showing with 31 additions and 2 deletions.
  1. +31 −2 src/main/clojure/clojure/core/cache.clj
@@ -182,6 +182,16 @@
(into {} (take (- limit (count base)) (for [k (range (- limit) 0)] [k k])))
(into {} (for [[k _] base] [k start-at]))))
+(comment
+
+ (-> (clojure.core.cache/lru-cache-factory {} :threshold 2)
+ (assoc :a 1)
+ (assoc :b 2)
+ (assoc :b 3)
+ )
+
+)
+
(defcache LRUCache [cache lru tick limit]
CacheProtocol
(lookup [_ item]
@@ -199,7 +209,9 @@
(miss [_ item result]
(let [tick+ (inc tick)]
(if-let [ks (keys lru)]
- (let [k (apply min-key lru ks) ;; maybe evict case
+ (let [k (if (contains? lru item)
+ item
+ (apply min-key lru ks)) ;; maybe evict case
sz (count ks)
c (if (>= sz limit)
(-> cache (dissoc k) (assoc item result))
@@ -271,6 +283,21 @@
(str cache \, \space ttl \, \space ttl-ms)))
+(comment
+
+ (-> (clojure.core.cache/lu-cache-factory {} :threshold 3)
+ (assoc :a 1)
+ (assoc :b 2)
+ (assoc :b 3)
+ (assoc :c 4)
+ (assoc :d 5)
+ (assoc :e 6)
+ ((juxt #(.cache %) #(.lu %) #(.limit %))))
+
+
+)
+
+
(defcache LUCache [cache lu limit]
CacheProtocol
(lookup [_ item]
@@ -283,7 +310,9 @@
(LUCache. cache (update-in lu [item] inc) limit))
(miss [_ item result]
(if-let [ks (keys lu)]
- (let [k (apply min-key lu ks) ;; maybe evict case
+ (let [k (if (contains? lu item)
+ ::nope
+ (apply min-key lu ks)) ;; maybe evict case
sz (count ks)
c (if (>= sz limit)
(-> cache (dissoc k) (assoc item result))

0 comments on commit 0b17c52

Please sign in to comment.