public interface Cache<K,V>
This class is an internal implementation detail only intended for use within spectator. It is subject to change without notice.
| Modifier and Type | Method and Description |
|---|---|
Map<K,V> |
asMap()
Returns a map containing a snapshot of the cache.
|
void |
clear()
Remove all entries from the cache.
|
V |
computeIfAbsent(K key,
Function<K,V> f)
Returns the cached value associated with the key if present, otherwise computes a value
using the provided function.
|
static <K1,V1> Cache<K1,V1> |
directMapped(Registry registry,
String id,
int initialSize,
int maxSize)
Create a new direct-mapped cache: an open-addressed table that evicts on hash collision, so
eviction is O(1) with no compaction sweep and reads and writes are lock-free.
|
V |
get(K key)
Returns the cached value associated with the key or null if none is found.
|
static <K1,V1> Cache<K1,V1> |
lfu(Registry registry,
String id,
int baseSize,
int compactionSize)
Create a new cache instance that removes the least frequently used items when full.
|
V |
peek(K key)
Like
get(Object), but does not update the access count. |
void |
put(K key,
V value)
Add or overwrite the cache entry for the key.
|
int |
size() |
static <K1,V1> Cache<K1,V1> lfu(Registry registry, String id, int baseSize, int compactionSize)
registry - Registry to use for tracking stats about the cache.id - Used with metrics to indentify a particular instance of the cache.baseSize - Number of items that should always be kept around in the cache.compactionSize - Maximum size of the underlying map for the cache.static <K1,V1> Cache<K1,V1> directMapped(Registry registry, String id, int initialSize, int maxSize)
initialSize to maxSize as its
working set fills it, so memory tracks live entries rather than the configured maximum. Writes
are best-effort (a collision or a race may drop an entry, causing a later miss but never an
incorrect result), which makes this a good fit for memoizing a pure function.registry - Registry to use for tracking stats about the cache.id - Used with metrics to identify a particular instance of the cache.initialSize - Initial number of slots allocated on the first write. Use a small value so lightly used
instances stay cheap.maxSize - Maximum number of slots the table can grow to. Use initialSize == maxSize for a
fixed-capacity, non-growing cache.V peek(K key)
get(Object), but does not update the access count.V computeIfAbsent(K key, Function<K,V> f)
key - Id to use for looking up a value in the cache.f - Function to compute a value based on the key. This function may get called multiple
times on a cache miss with some results getting discarded.void clear()
int size()