A in-memory, fixed-size cache for Clojure / Clojurescript. Keys and values are arbitrary Clojure data. Uses the Clock caching algorithm: https://en.wikipedia.org/wiki/Page_replacement_algorithm#Clock
All public functions are in the deercreeklabs.stockroom
namespace. All other namespaces should be considered private implementation
details that may change.
(stockroom num-keys)Creates a stockroom cache with the given capacity.
num-keys: The number of keys this cache will contain.
The created stockroom cache.
(def my-cache (stockroom 100))(get stockroom k)Gets the value for the given key from the cache.
stockroom: The stockroom cachek: The key to retrieve
The value of the given key, or nil if the key is not present
(get my-cache "a")(keys stockroom)Returns all keys in the cache as a sequence.
stockroom: The stockroom cache
All keys in the cache as a sequence.
(keys my-cache)(put! stockroom k v)Puts the given value for the given key into the cache. If the cache is full, evicts a key to make room.
stockroom: The stockroom cachek: The keyv: The value
nil
(put! my-cache :a-key "a-value")(evict! stockroom k)Evicts the given key from the cache.
stockroom: The stockroom cachek: The key
nil
(evict! my-cache "a key you don't want")(flush! stockroom)Flushes / empties the cache.
stockroom: The stockroom cache
nil
(flush! my-cache)(memoize-sr stockroom f)
(memoize-sr stockroom f num-keys)Memoizes the given function, based on the arguments passed to the function.
Optionally takes a num-keys argument which specifies the cache capacity.
If num-keys is not provided, a cache of size 100 is used.
stockroom: The stockroom cachenum-keys: The number of keys this cache will contain. Optional; defaults to 100.
The memoized function.
(memoize-sr stockroom an-expensive-fn 500)Original work (c) 2016 FarBetter, Inc. Modified work (c) 2017 Deer Creek Labs, LLC
Distributed under the Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt