clojure.core.cache

https://github.com/clojure/core.cache.git

git clone 'https://github.com/clojure/core.cache.git'

(ql:quickload :clojure.core.cache)
322

clojure.core.cache

core.cache is a Clojure contrib library providing the following features:

The clojure.core.cache namespace contains the immutable caches themselves. The clojure.core.cache.wrapped namespace contains the same API operating on caches wrapped in atoms, which is the “normal” use in the wild (introduced in 0.8.0).

core.cache is based on an old library named Clache that has been thoroughly deprecated.

Releases and Dependency Information

Latest stable release: 0.8.1

Leiningen dependency information:

[org.clojure/core.cache "0.8.1"]

Maven dependency information:

<dependency>
  <groupId>org.clojure</groupId>
  <artifactId>core.cache</artifactId>
  <version>0.8.1</version>
</dependency>

Example Usage

    (require '[clojure.core.cache :as cache])

    (def C1 (cache/fifo-cache-factory {:a 1, :b 2}))

    (def C1' (if (cache/has? C1 :c)
               (cache/hit C1 :c)
               (cache/miss C1 :c 42)))

    ;=> {:a 1, :b 2, :c 42}

    (cache/lookup C1' :c)

    ;=> 42

    (get C1' :c) ; cache/lookup is implemented as get

    ;=> 42

    ;; a shorthand for the above conditional...
    (def C1' (cache/through-cache C1 :c (constantly 42)))
    ;; ...which uses a value to compute the result from the key...
    (cache/through-cache C1 my-key (partial jdbc/get-by-id db-spec :storage))
    ;; ...so you could fetch values from a database if they're not in cache...

    (cache/evict C1 :b)

    ;=> {:a 1}

    ;; since the caches are immutable, you would normally wrap them in an atom
    (def C2 (atom (cache/fifo-cache-factory {:a 1, :b 2})))

    (swap! C2 cache/through-cache :d (constantly 13))

    ;=> {:a 1, :b 3, :d 13}

    (swap! C2 cache/evict :b)

    ;=> {:a 1, :d 13}

    (get @C2 :a)

    ;=> 1

    ;; or use the wrapped API instead:
    (require '[clojure.core.cache.wrapped :as c])

    (def C3 (c/fifo-cache-factory {:a 1, :b 2}))

    (c/through-cache C3 :d (constantly 13)) ; returns updated cache

    ;=> {:a 1, :b 3, :d 13}

    (c/evict C3 :b)

    ;=> {:a 1, :d 13}

    (c/lookup C3 :a) ; or (get @C3 :a)

    ;=> 1

    ;; unique to the wrapped API:
    (c/lookup-or-miss C3 :b (constantly 42))

    ;=> 42

    @C3

    ;=> {:a 1, :d 13, :b 42}

Refer to docstrings in the clojure.core.cache or clojure.core.cache.wrapped namespaces, or the autogenerated API documentation for additional documentation.

Developer Information

Change Log

Copyright and License

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 (http://opensource.org/licenses/eclipse-1.0.php) 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.