Skip to content
Browse files

FIFO fencepost fix

  • Loading branch information...
1 parent bcd4faf commit b705f56b3e8e6d1bab97e1411e264fed48bc3694 @fogus fogus committed Aug 28, 2012
Showing with 87 additions and 72 deletions.
  1. +10 −0 docs/plans.org
  2. +65 −65 pom.xml
  3. +6 −6 src/main/clojure/clojure/core/cache.clj
  4. +6 −1 src/test/clojure/clojure/core/cache/tests.clj
View
10 docs/plans.org
@@ -0,0 +1,10 @@
+* Plans for core.cache
+
+** Asynchronous caching protocol
+** LIRSCache evict
+** Adaptive eviction caching
+** Function-backed cache
+** Random eviction cache
+** test.generative usage
+** Explore Arc impl.
+** More documentation and examples
View
130 pom.xml
@@ -1,65 +1,65 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <artifactId>core.cache</artifactId>
- <version>0.6.3-SNAPSHOT</version>
- <name>${artifactId}</name>
- <description>A cache library for Clojure</description>
- <packaging>jar</packaging>
-
- <licenses>
- <license>
- <name>Eclipse Public License 1.0</name>
- <url>http://opensource.org/licenses/eclipse-1.0.php</url>
- <distribution>repo</distribution>
- </license>
- </licenses>
-
- <parent>
- <groupId>org.clojure</groupId>
- <artifactId>pom.contrib</artifactId>
- <version>0.0.26</version>
- </parent>
-
- <developers>
- <developer>
- <id>fogus</id>
- <name>Fogus</name>
- <url>http://fogus.me</url>
- </developer>
- </developers>
-
- <!-- <dependencies> -->
- <!-- <dependency> -->
- <!-- <groupId>org.clojure</groupId> -->
- <!-- <artifactId>test.generative</artifactId> -->
- <!-- <version>0.1.4-SNAPSHOT</version> -->
- <!-- <optional>true</optional> -->
- <!-- </dependency> -->
- <!-- </dependencies> -->
-
- <issueManagement>
- <system>jira</system>
- <url>http://dev.clojure.org/jira/browse/CCACHE</url>
- </issueManagement>
-
- <scm>
- <connection>scm:git:git://github.com/clojure/core.cache.git</connection>
- <url>http://github.com/clojure/core.cache</url>
- </scm>
-
- <repositories>
- <repository>
- <id>sonatype-oss-snapshots</id>
- <url>https://oss.sonatype.org/content/repositories/snapshots</url>
- </repository>
- </repositories>
-
- <build>
- <resources>
- <resource>
- <directory>src/test/clojure</directory>
- </resource>
- </resources>
- </build>
-</project>
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>core.cache</artifactId>
+ <version>0.6.3-SNAPSHOT</version>
+ <name>${artifactId}</name>
+ <description>A cache library for Clojure</description>
+ <packaging>jar</packaging>
+
+ <licenses>
+ <license>
+ <name>Eclipse Public License 1.0</name>
+ <url>http://opensource.org/licenses/eclipse-1.0.php</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+
+ <parent>
+ <groupId>org.clojure</groupId>
+ <artifactId>pom.contrib</artifactId>
+ <version>0.0.26</version>
+ </parent>
+
+ <developers>
+ <developer>
+ <id>fogus</id>
+ <name>Fogus</name>
+ <url>http://fogus.me</url>
+ </developer>
+ </developers>
+
+ <!-- <dependencies> -->
+ <!-- <dependency> -->
+ <!-- <groupId>org.clojure</groupId> -->
+ <!-- <artifactId>test.generative</artifactId> -->
+ <!-- <version>0.1.4-SNAPSHOT</version> -->
+ <!-- <optional>true</optional> -->
+ <!-- </dependency> -->
+ <!-- </dependencies> -->
+
+ <issueManagement>
+ <system>jira</system>
+ <url>http://dev.clojure.org/jira/browse/CCACHE</url>
+ </issueManagement>
+
+ <scm>
+ <connection>scm:git:git://github.com/clojure/core.cache.git</connection>
+ <url>http://github.com/clojure/core.cache</url>
+ </scm>
+
+ <repositories>
+ <repository>
+ <id>sonatype-oss-snapshots</id>
+ <url>https://oss.sonatype.org/content/repositories/snapshots</url>
+ </repository>
+ </repositories>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/test/clojure</directory>
+ </resource>
+ </resources>
+ </build>
+</project>
View
12 src/main/clojure/clojure/core/cache.clj
@@ -155,12 +155,12 @@
(hit [this item]
this)
(miss [_ item result]
- (let [[cache q] (if (>= (count cache) limit)
- (let [k (peek q)]
- [(dissoc cache k) (pop q)])
- [cache q])]
- (FIFOCache. (assoc cache item result)
- (conj q item)
+ (let [[kache qq] (let [k (peek q)]
+ (if (>= (count cache) limit)
+ [(dissoc cache k) (pop q)]
+ [cache (pop q)]))]
+ (FIFOCache. (assoc kache item result)
+ (conj qq item)
limit)))
(evict [this key]
(let [v (get cache key ::miss)]
View
7 src/test/clojure/clojure/core/cache/tests.clj
@@ -123,7 +123,12 @@
(testing "that finding works for FifoCache"
(do-finding (FIFOCache. small-map clojure.lang.PersistentQueue/EMPTY 2)))
(testing "that contains? works for FifoCache"
- (do-contains (FIFOCache. small-map clojure.lang.PersistentQueue/EMPTY 2))))
+ (do-contains (FIFOCache. small-map clojure.lang.PersistentQueue/EMPTY 2)))
+ (testing "that FIFO caches starting with less elements than the threshold work"
+ (let [C (fifo-cache-factory {:a 1, :b 2} :threshold 3)]
+ (are [x y] (= x y)
+ {:a 1, :b 2, :c 3} (.cache (assoc C :c 3))
+ {:d 4, :b 2, :c 3} (.cache (assoc C :c 3 :d 4))))))
(deftest test-lru-cache-ilookup
(testing "that the LRUCache can lookup via keywords"

0 comments on commit b705f56

Please sign in to comment.
Something went wrong with that request. Please try again.