Package org.naviqore.utils.cache
Class EvictionCache<K,V>
java.lang.Object
org.naviqore.utils.cache.EvictionCache<K,V>
- Type Parameters:
K- the type of keys maintained by this cacheV- the type of mapped values
A generic cache that supports LRU (Least Recently Used) and MRU (Most Recently Used) eviction strategies.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum representing the eviction strategy. -
Constructor Summary
ConstructorsConstructorDescriptionEvictionCache(int size, EvictionCache.Strategy strategy) Constructs a new EvictionCache with the specified size and eviction strategy. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the cache, removing all key-value mappings.computeIfAbsent(K key, Supplier<V> supplier) If the specified key is not already associated with a value (or is mapped tonull), attempts to compute its value using the given supplier and enters it into this cache.intReturns the current number of entries in the cache.booleanChecks if the specified key is present in the cache.
-
Constructor Details
-
Method Details
-
computeIfAbsent
If the specified key is not already associated with a value (or is mapped tonull), attempts to compute its value using the given supplier and enters it into this cache.- Parameters:
key- the key with which the specified value is to be associatedsupplier- the supplier function to compute the value- Returns:
- the current (existing or computed) value associated with the specified key
-
clear
public void clear()Clears the cache, removing all key-value mappings. -
isCached
Checks if the specified key is present in the cache.- Parameters:
key- the key whose presence in this cache is to be tested- Returns:
trueif this cache contains a mapping for the specified key
-
getNumberOfEntries
public int getNumberOfEntries()Returns the current number of entries in the cache.
-