Package nl.colorize.util.stats
Class Cache<K,V>
java.lang.Object
nl.colorize.util.stats.Cache<K,V>
Data structure for key/value pairs, where the value for each key is based on
an underlying compute function. The cache is lazy: values are only computed
when first requested. If the cache capacity has been exceeded, the oldest
values are removed. Note that "oldest" means the values that were least
recently *calculated*, not the values that were least recently *accessed*.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> Cache <K, V> Creates a lookup table based on the specified function.static <K,
V> Cache <K, V> Creates a lookup table based on the specified function.Returns the value mapped to the specified key.void
Forgets all cached key/value pairs in this cache.void
invalidate
(K key) Forgets a cached key/value pairs in this cache.protected boolean
Returns true if the value for the specified key is currently cached, and false if the value still needs to be computed.void
precompute
(Iterable<K> keys) Precomputes the values for the specified keys, so the cached values are used when the key/value pairs are retrieved at a later time.void
precompute
(K key) Precomputes the value for the specified key, so the cached value is used when the key/value pair is retrieved at a later time.toString()
-
Method Details
-
get
Returns the value mapped to the specified key. If the key/value pair is currently present in the cache, the cached value will be returned. Otherwise, the underlying function is used to compute the value, cache it, then return it. -
precompute
Precomputes the value for the specified key, so the cached value is used when the key/value pair is retrieved at a later time. -
precompute
Precomputes the values for the specified keys, so the cached values are used when the key/value pairs are retrieved at a later time. -
invalidate
Forgets a cached key/value pairs in this cache. If the key/value pair was not yet computed this method does nothing. -
invalidate
public void invalidate()Forgets all cached key/value pairs in this cache. -
isCached
Returns true if the value for the specified key is currently cached, and false if the value still needs to be computed. -
toString
-
from
Creates a lookup table based on the specified function. The lookup table will have unlimited capacity, cached values will never be removed. -
from
Creates a lookup table based on the specified function. The lookup table will have limited capacity. If the number of key/value pairs exceeds the capacity, the oldest values will be removed.
-