| Modifier and Type | Method and Description |
|---|---|
<K1 extends K,V1 extends V> |
build()
Builds a
Cache which does not automatically load values when keys are requested. |
<K1 extends K,V1 extends V> |
build(Function<? super K1,? extends V1> mappingFunction)
|
static CacheBuilder<Object,Object> |
builder()
Creates a new
CacheBuilder with default settings, including strong keys, strong values, and no automatic
eviction of any kind. |
static <K,V> Cache<K,V> |
empty()
Returns an immutable empty
Cache that does nothing. |
CacheBuilder<K,V> |
initialCapacity(int initialCapacity)
Sets the minimum total size for the internal data structures.
|
CacheBuilder<K,V> |
maximumSize(long maximumSize)
Specifies the maximum number of entries the cache may contain.
|
<K1 extends K,V1 extends V> |
maximumWeight(long maximumWeight,
ToIntBiFunction<? super K1,? extends V1> weigher)
Specifies the maximum weight of entries the cache may contain.
|
CacheBuilder<K,V> |
recordStats()
Enables the accumulation of
CacheStats during the operation of the cache. |
CacheBuilder<K,V> |
softValues()
Specifies that each value (not key) stored in the cache should be wrapped in a
SoftReference (by default,
strong references are used). |
CacheBuilder<K,V> |
weakKeys()
Specifies that each key (not value) stored in the cache should be wrapped in a
WeakReference (by default,
strong references are used). |
CacheBuilder<K,V> |
weakValues()
Specifies that each value (not key) stored in the cache should be wrapped in a
WeakReference (by default,
strong references are used). |
@Nonnull static <K,V> Cache<K,V> empty()
Cache that does nothing.K - the type of keys maintained by this cacheV - the type of mapped values@Nonnull static CacheBuilder<Object,Object> builder()
CacheBuilder with default settings, including strong keys, strong values, and no automatic
eviction of any kind.@Nonnull CacheBuilder<K,V> recordStats()
CacheStats during the operation of the cache.
Without this Cache.stats() will return zero for all statistics. Note that recording statistics requires
bookkeeping to be performed with each operation, and thus imposes a performance penalty on cache operation.
@Nonnull CacheBuilder<K,V> initialCapacity(@Nonnegative int initialCapacity)
Providing a large enough estimate at construction time avoids the need for expensive resizing operations later, but setting this value unnecessarily high wastes memory.
initialCapacity - minimum total size for the internal data structuresIllegalArgumentException - if initialCapacity is negativeIllegalStateException - if an initial capacity was already set@Nonnull CacheBuilder<K,V> maximumSize(@Nonnegative long maximumSize)
Note that the cache may evict an entry before this limit is exceeded or temporarily exceed the threshold while evicting. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.
When size is zero, elements will be evicted immediately after being loaded into the cache. This can be
useful in testing, or to disable caching temporarily without a code change.
This feature cannot be used in conjunction with maximumWeight(long, java.util.function.ToIntBiFunction<? super K1, ? extends V1>).
maximumSize - the maximum size of the cacheIllegalArgumentException - if maximumSize is negativeIllegalStateException - if a maximum size or weight was already set<K1 extends K,V1 extends V> CacheBuilder<K,V> maximumWeight(@Nonnegative long maximumWeight, ToIntBiFunction<? super K1,? extends V1> weigher)
Weight is determined using the ToIntBiFunction specified with weigher. Weights are measured and
recorded when entries are inserted into or updated in the cache, and are thus effectively static during the
lifetime of a cache entry.
Note that the cache may evict an entry before this limit is exceeded or temporarily exceed the threshold while evicting. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.
When maximumWeight is zero, elements will be evicted immediately after being loaded into cache. This can
be useful in testing, or to disable caching temporarily without a code change.
Note that weight is only used to determine whether the cache is over capacity; it has no effect on selecting which entry should be evicted next.
This feature cannot be used in conjunction with maximumSize(long).
maximumWeight - the maximum total weight of entries the cache may containweigher - the weigher to use in calculating the weight of cache entriesIllegalArgumentException - if maximumWeight is negativeIllegalStateException - if a maximum weight or size was already set@Nonnull CacheBuilder<K,V> weakKeys()
WeakReference (by default,
strong references are used).
WARNING: when this method is used, the resulting cache will use identity (==) comparison to
determine equality of keys.
IllegalStateException - if the key strength was already set or the writer was set@Nonnull CacheBuilder<K,V> weakValues()
WeakReference (by default,
strong references are used).
Weak values will be garbage collected once they are weakly reachable. This makes them a poor candidate for
caching; consider softValues() instead.
WARNING: when this method is used, the resulting cache will use identity (==) comparison to
determine equality of values.
Entries with values that have been garbage collected may be counted in Cache.size(), but will never be
visible to read or write operations; such entries are cleaned up as part of the routine maintenance described in
the class javadoc.
IllegalStateException - if the value strength was already set@Nonnull CacheBuilder<K,V> softValues()
SoftReference (by default,
strong references are used).
Softly-referenced objects will be garbage-collected in a globally least-recently-used manner, in response to memory demand.
WARNING: when this method is used, the resulting cache will use identity (==) comparison to
determine equality of values.
IllegalStateException - if the value strength was already set@Nonnull <K1 extends K,V1 extends V> Cache<K1,V1> build()
Cache which does not automatically load values when keys are requested.
Consider build(Function) instead, if it is feasible to implement a CacheLoader.
K1 - the key type of the cacheV1 - the value type of the cache@Nonnull <K1 extends K,V1 extends V> Cache<K1,V1> build(Function<? super K1,? extends V1> mappingFunction)
Cache, which either returns an already-loaded value for a given key or atomically computes or
retrieves it using the supplied Function. If another thread is currently loading the value for this key,
simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently
load values for distinct keys.K1 - the key type of the loaderV1 - the value type of the loadermappingFunction - the function used to obtain new valuesCopyright © 2017–2019 Atlanmod. All rights reserved.