README.md

clj-cache

This library is deprecated. Please use the standard library core.cache instead.

A Clojure library that caches the results of impure functions. This library provides 3 internal caching strategies and can also cache externally or persistently using the java ehcache package.

I have found this useful for caching the results of database calls and for holding HTML snippets.

This library is available at clojars.org for use with Leiningen, Cake or Maven.

:dependencies [[clj-cache "0.0.4"]]

The internal caching functions consist of small modifications to the memoization functions described in these two excellent blog posts, the rule of three and memoize done right. I'd recommend these posts to Clojure programmers as they discuss flexible apis and concurrency in real world detail.

Example

(ns an-example
(:use clj-cache.cache))
(defn-cached get-user-from-db
(lru-cache-strategy 1000)
"Gets a user details from a database. Caches the last 1000
users read in i.e support serving a 1000 concurrent users
from memory."
[username]
;; Slow database read goes here
)
;; First read of the user is slow
(get-user-from-db "fred")
;; Second is fast
(get-user-from-db "fred")
;; When fred's details are changed invalidate the cache
(invalidate-cache get-user-from-db "fred")
;; The next call will read from the db and cache the result again
(get-user-from-db "fred")

Available Functions

cached

invalidate-cache

([cached-f & args])
Invalidates the cache for the function call with the given arguments
causing it to be re-evaluated e.g
(invalidate-cache fib 30) ;; A call to (fib 30) will not use the cache
(invalidate-cache fib 29) ;; A call to (fib 29) will not use the cache
(fib 18) ;; A call to (fib 18) will use the cache