Liking cljdoc? Tell your friends :D

core.memoize

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)
  42)

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

(memo-calc)
;; wait 5 seconds
;;=> 42

(memo-calc)
;; 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.

Overview

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.*

Places

Can you improve this documentation? These fine people already did:
Fogus, Sean Corfield & fogus
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close