Latest commit 3ae57a0 Jul 24, 2013 @fogus fogus Added note about CG an ED
Failed to load latest commit information.
release-notes First cut at docs Building page Jul 5, 2013 Added note about CG an ED Jul 24, 2013


Table of Topics

The problem

Value caching is sometimes needed. This need is often driven by the desire is to avoid calculating expensive operations such as inherently costly algorithms more often than necessary. The naive solution for this need is to perform some expensive operation once and cache the result. Therefore, whenever the same calculation is needed in the future it can be retrieved from cache more quickly than simply recalculating from scratch.

Clojure provides a default way to cache the results of function calls using the memoize function:

(defn slow-calc []
  (Thread/sleep 5000)

(def memo-calc (memoize slow-calc))

;; wait 5 seconds
;;=> 42

;; instantly
;;=> 42

While appropriate for many problems, the naive caching provided by memoize can consume available memory as it never releases stored values. Therefore, the ideal situation is to expunge stored results that have expired, meant for single-use or less likely to be needed again. There are many general-purpose and domain-specific strategies for efficient cache population and eviction. The core.memoize library provides implementations of common caching strategies for use in memoization scenarios.


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

The implementation of core.memoize is based on and heavily influenced by the excellent 'Memoize done right' by Meikel Brandmeyer and the surrounding discussion with Christophe Grand and Eugen Dück.*