Skip to content
fogus edited this page Aug 14, 2012 · 23 revisions

Using core.cache

note: see the page on Including core.cache before you begin this section

For any of the cache implementations, the pattern of usage is as follows:

    (require '[clojure.core.cache :as cache])
	
    (def C (cache/fifo-cache-factory {:a 1, :b 2})
	
    (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}

Using the has?/hit/miss pattern ensures that the thresholding and eviction logic for each implementation works properly. Avoid this pattern at your own risk.

Builtin cache implementations

core.cache comes with a number of builtin immutable cache implementations, including (click through for specific information):

The core.cache implementations are backed by any map-like object. Additionally, each cache implements the Clojure map behaviors and can therefore serve as special maps or even as backing stores for other cache implementations. For caches taking a limit argument, the eviction policies tend not to apply until the limit has been exceeded.

Extending core.cache

See the section on creating custom caches for more information.

Clone this wiki locally