clojure.core.cache
A caching library for Clojure.
CacheProtocol
This is the protocol describing the basic cache capability.
(fifo-cache-factory base & {threshold :threshold, :or {threshold 32}})
Returns a FIFO cache with the cache and FIFO queue initialized to `base` --
the queue is filled as the values are pulled out of `base`. If the associative
structure can guarantee ordering, then the said ordering will define the
eventual eviction order. Otherwise, there are no guarantees for the eventual
eviction ordering.
This function takes an optional `:threshold` argument that defines the maximum number
of elements in the cache before the FIFO semantics apply (default is 32).
If the number of elements in `base` is greater than the limit then some items
in `base` will be dropped from the resulting cache. If the associative
structure used as `base` can guarantee sorting, then the last `limit` elements
will be used as the cache seed values. Otherwise, there are no guarantees about
the elements in the resulting cache.
(has? cache e)
Checks if the cache contains a value associated with `e`
(hit cache e)
Is meant to be called if the cache is determined to contain a value
associated with `e`
(lookup cache e)
(lookup cache e not-found)
Retrieve the value associated with `e` if it exists, else `nil` in
the 2-arg case. Retrieve the value associated with `e` if it exists,
else `not-found` in the 3-arg case.
(lru-cache-factory base & {threshold :threshold, :or {threshold 32}})
Returns an LRU cache with the cache and usage-table initialied to `base` --
each entry is initialized with the same usage value.
This function takes an optional `:threshold` argument that defines the maximum number
of elements in the cache before the LRU semantics apply (default is 32).
(miss cache e ret)
Is meant to be called if the cache is determined to **not** contain a
value associated with `e`
(seed cache base)
Is used to signal that the cache should be created with a seed.
The contract is that said cache should return an instance of its
own type.
(soft-cache-factory base)
Returns a SoftReference cache. Cached values will be referred to with
SoftReferences, allowing the values to be garbage collected when there is
memory pressure on the JVM.
SoftCache is a mutable cache, since it is always based on a
ConcurrentHashMap.
(ttl-cache-factory base & {ttl :ttl, :or {ttl 2000}})
Returns a TTL cache with the cache and expiration-table initialized to `base` --
each with the same time-to-live.
This function also allows an optional `:ttl` argument that defines the default
time in milliseconds that entries are allowed to reside in the cache.