-
Notifications
You must be signed in to change notification settings - Fork 63
Using
note: see the page on Including core.cache before you begin this section
To use the cache implementations or extend the core.cache protocols you first need to require the proper namespace:
(require '[clojure.core.cache :as cache])
Next you should create an instance of a specific cache type, optionally seeded:
(def C (cache/fifo-cache-factory {:a 1, :b 2})
To ensure the proper cache policies are followed for each specific type, the following has?->hit,miss
pattern should be used:
(if (cache/has? C :c)
(cache/hit C :c)
(cache/miss C :c 42))
;=> {:a 1, :b 2, :c 42}
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.
Finally, to explicitly evict an element in a cache, use the evict
function:
(cache/evict C :b)
;=> {:a 1}
For specific information about eviction policies and thresholds, view the specific documentation for each cache type listed in the next section.
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.
See the section on creating custom caches for more information.