Caches and regions of the :memento.core/guava type are backed by this implementation.
Besides the standard configuration keys, key-fn, ret-fn, seed, ..., this implementation allows additional configuration:
AS MENTIONED, YOU CAN USE NON-NAMESPACED KEYS
Some of the properties require that you specify a duration. In these cases you can:
seconds will be used[int, unit-keyword]The units are, from nano-seconds to days: :ns, :us, :ms, :s, :m, :h, :d
:memento.core/concurrencyCorresponds to .concurrencyLevel on CacheBuilder. Integer describing
how many Sections the cache has, different Sections being able to be
concurrently accessed. Larger number = less locking in concurrent access.
Default is 4.
:memento.core/initial-capacityCorresponds to .initialCapacity on CacheBuilder. Sets the initial capacity of the new cache.
Default is 16.
:memento.core/size<Corresponds to .maxSize on CacheBuilder. Sets the size of cache that should not
be exceeded, value an integer denoting max count of items. It cannot be used at the same time as weight based eviction.
Guava might evict before reaching the limit and the eviction order appears to be a mix of LRU and LU, per Guava documentation:
The cache will try to evict entries that haven't been used recently or very often.
Warning: the cache may evict entries before this limit is exceeded,
-- typically when the cache size is approaching the limit.
:memento.core/weight< and :memento.core/kv-weightAn alternative to size< for cache size based eviction, similar in behaviour as above, except:
The :weight< integer setting specifies max number of weight units the cache can take.
The :kv-weight should be a function of 2-arguments transformed key, transformed value and return an integer, the number of
weight units this entry should take.
Note: key and value passed to the weight function are the values after key-fn and ret-fn have been applied
:memento.core/weak-valuesCorresponds to .weakValues on CacheBuilder.
Boolean flag, enabling storing values using weak references.
This allows entries to be garbage-collected if there are no other (strong or soft) references to the values.
:memento.core/soft-valuesCorresponds to .softValues on CacheBuilder. Boolean flag, enabling storing values using soft references.
Softly referenced objects are garbage-collected in a globally least-recently-used manner, in response to memory demand. Because of the performance implications of using soft references, we generally recommend using the more predictable maximum cache size instead.
:memento.core/ttlCorresponds to .expireAfterWrite on CacheBuilder.
Duration type property (e.g. [15 :s] or 15)
Expire entries after the specified duration has passed since the entry was created, or the most recent replacement of the value. This could be desirable if cached data grows stale after a certain amount of time.
Timed expiration is performed with periodic maintenance during writes and occasionally during reads.
:memento.core/fadeCorresponds to .expireAfterAccess on CacheBuilder.
Duration type property (e.g. [15 :s] or 15).
Only expire entries after the specified duration has passed since the entry was last accessed by a read or a write. Note that the order in which entries are evicted will be similar to that of size-based eviction.
:memento.core/tickerCorresponds to .ticker on CacheBuilder.
A function of zero arguments that should return current nano time. This is used when doing time based eviction.
The default is (fn [] (System/nanoTime)).
This is useful for testing and you can also make the time move in discrete amounts (e.g. you can make all cache accesses in a request have same time w.r.t. eviction).
:memento.core/removal-listenerCorresponds to .removalListener on CacheBuilder.
A function of three arguments, that will be called whenever an entry is removed. The three arguments are:
key-fn was applied)ret-fn was applied):memento.core/statsBoolean flag, enabling collection of stats.
Corresponds to .enableStats on CacheBuilder.
You can retrieve a cache's stats by using memento.guava/stats:
(memento.guava/stats my-cached-function)
Returns com.google.common.cache.CacheStats instance or nil.
Guava properties when configuring a Region are the same.
The main differences:
Whereas in Cache, the transformed key is the argument list with key-fn applied to it,
the guava Region has a more complex transformed key.
The region transformed key is a vector of [f args] where f is the function that the args
applies to and args is the argument list after being processed by Cache's key-fn and
region's key-fn.
The values have had Cache's ret-fn and Region's ret-fn applied to.
:memento.core/weight< and :memento.core/kv-weightAn alternative to size< for cache size based eviction, similar in behaviour as above, except:
The :weight< integer setting specifies max number of weight units the cache can take.
The :kv-weight should be a function of 3 arguments:
and return an integer, the number of weight units this entry should take.
:memento.core/removal-listenerCorresponds to .removalListener on CacheBuilder.
A function of four arguments, that will be called whenever an entry is removed. The three arguments are:
key-fn was applied)ret-fn was applied)Instead of (memento.guava/stats my-cached-function) you need to use
(memento.guava/region-stats :region-id).
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |