Browse files

Modified FifoCache to respect threshold. Modified README to conform t…

…o contrib format
  • Loading branch information...
1 parent dd9439d commit 8c22ef153b7825b33e73cea41607d106fd3d0268 @fogus fogus committed Aug 6, 2012
Showing with 63 additions and 60 deletions.
  1. +56 −56
  2. +7 −4 src/main/clojure/clojure/core/cache.clj
@@ -1,17 +1,19 @@
-# clojure.core.cache - A Clojure Caching Library
core.cache is a new Clojure contrib library providing the following features:
* An underlying `CacheProtocol` used as the base abstraction for implementing new synchronous caches
* A `defcache` macro for hooking your `CacheProtocol` implementations into the Clojure associative data capabilities.
-* Immutable implementations of some basic caching strategies
+* Implementations of some basic caching strategies
- First-in-first-out (FIFOCache)
- Least-recently-used (LRUCache)
- Least-used (LUCache)
- Time-to-live (TTLCache)
- Naive cache (BasicCache)
+ - Naive cache backed with soft references (SoftCache)
* Implementation of an efficient buffer replacement policy based on the *low inter-reference recency set* algorithm (LIRSCache) described in the [LIRS]( paper
@@ -20,83 +22,81 @@ core.cache is a new Clojure contrib library providing the following features:
core.cache is based on a library named Clache, found at that is planned for deprecation.
+Releases and Dependency Information
-You can use core.cache in your [Leiningen]( and [Cake]( projects with the following `:dependencies` directive in your `project.clj` file:
+Latest stable release: 0.6.2
- [org.clojure/core.cache "0.5.0"]
+* [All Released Versions](
-For Maven-driven projects, use the following slice of XML in your `pom.xml`'s `<dependencies>` section:
+* [Development Snapshot Versions](;gav~org.clojure.cache~~~)
- <dependency>
- <groupId>org.clojure</groupId>
- <artifactId>core.cache</artifactId>
- <version>0.5.0</version>
- </dependency>
+[Leiningen]( dependency information:
-* [Source code](
-* [Ticket system](
-* [Announcement](
-* [Examples and documentation](
+ [org.clojure/data.json "0.1.2"]
+[Maven]( dependency information:
-Changes from Clache
-The v0.5.0 version of core.cache is based almost wholly on the final version of Clache, with the following changes:
-* An addition of an `evict` function on the `CacheProtocol` used to explicitly remove a value from a cache based on a key. All of the existing cache types implement this function *except* for `LIRSCache`.
+ <dependency>
+ <groupId>org.clojure</groupId>
+ <artifactId>core.cache</artifactId>
+ <version>0.6.2</version>
+ </dependency>
-* The addition of cache factory functions for all of the existing cache types
-* The associative structure behaviors are defined solely in terms of the underlying `CacheProtocol`
-* The `SoftCache` implementation was buggy and removed for now
+Example Usage
+ (require [clojure.core.cache :as cache])
+ (def C (cache/fifo-cache-factory {:a 1, :b 2})
jochenberger added a line comment Aug 7, 2014

I think there's a closing paren missing

Clojure member
fogus added a line comment Aug 7, 2014

Added. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ (if (cache/has? C :c)
+ (cache/hit C :c)
+ (cache/miss C :c 42))
+ ;=> {:a 1, :b 2, :c 42}
+ (cache/evict C :b)
+ ;=> {:a 1}
+Refer to docstrings in the `clojure.core.cache` namespace, or the [autogenerated API documentation]( for additional documentation
-The following capabilities are under design, development, or consideration for future versions of core.cache:
-* Asynchronous caching protocol
-* `LIRSCache evict`
-* Removal of the `seed` function from the `CacheProtocol`
-* Reimplementation of a cache based on soft references
-* test.generative usage
-* Deprecation of Clache
-* Documentation and examples
-More planning is needed around capabilities not listed nor thought of.
+Developer Information
+* [GitHub project](
+* [Bug Tracker](
-core.cache would not exist if not for the inspiration and contributions of the following people:
+* [Continuous Integration](
-* [Meikel Brandmeyer](
-* [Fogus](
-* [Sebastián Galkin](
-* [Jan Oberhagemann](
+* [Compatibility Test Matrix](
+Change Log
+* Release 0.6.2 on 2012.08.07 [more information](
+ * Removed reflection warnings
+ * Fixed eviction of items from LU, TTL and LRU caches with thresholds less than two
+ * Fixed eviction of items from FIFO cache prior to threshold
+* Release 0.6.1 on 2012.07.13 [more information](
+ * Added SoftCache
+ * Fixed eviction of items from LU and LRU caches prior to threshold
+ * Adjusted default thresholds in factory functions
+* Release 0.5.0 on 2011.12.13 [more information](
+ * Added `evict`
+ * Added cache factory functions
+ * Added associatve operation support
-Copyright © 2011 Rich Hickey
+Copyright and License
-Licensed under the EPL. (See the file epl.html.)
+Copyright (c) Rich Hickey, Michael Fogus and contributors, 2012. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 ( which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound bythe terms of this license. You must not remove this notice, or any other, from this software.
11 src/main/clojure/clojure/core/cache.clj
@@ -155,9 +155,12 @@
(hit [this item]
(miss [_ item result]
- (let [k (peek q)]
- (FIFOCache. (-> cache (dissoc k) (assoc item result))
- (-> q pop (conj item))
+ (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)
(evict [this key]
(let [v (get cache key ::miss)]
@@ -566,7 +569,7 @@
[base & {threshold :threshold :or {threshold 32}}]
{:pre [(number? threshold) (< 0 threshold)
(map? base)]
- :post [(== threshold (count %))]}
+ :post [(== threshold (count (.q ^FIFOCache %)))]}
(clojure.core.cache/seed (FIFOCache. {} clojure.lang.PersistentQueue/EMPTY threshold) base))
(defn lru-cache-factory

0 comments on commit 8c22ef1

Please sign in to comment.