Class MemoryAwareCacheSupport<K,V>
java.lang.Object
io.fluxzero.common.caching.MemoryAwareCacheSupport<K,V>
- Type Parameters:
K- key typeV- value type
- All Implemented Interfaces:
AutoCloseable
A hard-reference, weighted cache with LRU eviction and optional key ordering.
The cache intentionally avoids soft references. Memory ownership is explicit through entry weights and a maximum
total weight. A MemoryPressureController may request extra trimming when heap or GC pressure is observed.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher) MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator) MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator, MemoryPressureController memoryPressureController) MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator, MemoryPressureController memoryPressureController, Duration memoryPressureCheckInterval) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidclose()booleancontainsKey(K key) evict(K key, MemoryAwareCacheSupportEviction.Reason reason) Removes a value and publishes the supplied eviction reason when the key was present.voidevictBefore(K frontier, boolean inclusive) Evicts all ordered keys before the supplied frontier.firstKey()keys()lastKey()longlongbooleanStores a value when it fits the configured entry and total weight constraints.voidStores all values in iteration order while applying the same admission and weight constraints asput(Object, Object).intsize()booleanProactively sheds cached weight according to the configured trim ratio and maximum trim weight when the controller observes memory pressure.valuesFrom(K minKey, boolean inclusive, int maxSize) Returns cached values ordered by key, starting atminKey.longweight()
-
Field Details
-
DEFAULT_MEMORY_PRESSURE_CHECK_INTERVAL
-
-
Constructor Details
-
MemoryAwareCacheSupport
public MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher) -
MemoryAwareCacheSupport
public MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator) -
MemoryAwareCacheSupport
public MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator, MemoryPressureController memoryPressureController) -
MemoryAwareCacheSupport
public MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator, MemoryPressureController memoryPressureController, Duration memoryPressureCheckInterval)
-
-
Method Details
-
put
-
putAll
Stores all values in iteration order while applying the same admission and weight constraints asput(Object, Object).The cache is mutated under one lock and memory pressure is checked once after the full batch, rather than once per entry.
- Parameters:
values- values to store by cache key
-
get
-
containsKey
-
remove
-
evict
Removes a value and publishes the supplied eviction reason when the key was present. -
clear
public void clear() -
size
public int size() -
weight
public long weight() -
keys
-
firstKey
-
lastKey
-
maxWeight
public long maxWeight() -
maxEntryWeight
public long maxEntryWeight() -
valuesFrom
-
evictBefore
Evicts all ordered keys before the supplied frontier. -
registerEvictionListener
public Registration registerEvictionListener(Consumer<MemoryAwareCacheSupportEviction<K, V>> listener) -
trimForMemoryPressure
public boolean trimForMemoryPressure()Proactively sheds cached weight according to the configured trim ratio and maximum trim weight when the controller observes memory pressure.- Returns:
trueif pressure was observed
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-