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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumUnit represented by a cache weight. -
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) MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator, MemoryPressureController memoryPressureController, Duration memoryPressureCheckInterval, MemoryAwareCacheSupport.WeightUnit weightUnit) -
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()<U> voidsupplyAll(Iterable<? extends U> lookups, Function<? super U, ? extends K> keyFunction, BiConsumer<? super U, ? super V> valueConsumer) Supplies present values for an ordered group of lookups under one cache lock.booleanProactively sheds cached weight according to the configured trim ratio and maximum trim weight when the controller observes memory pressure.<U> voidupdateAll(Iterable<? extends U> updates, Function<? super U, ? extends K> keyFunction, BiFunction<? super U, ? super V, ? extends V> updateFunction) Applies ordered updates under one cache lock and checks memory pressure once after the batch.<U> voidupdateAll(Iterable<? extends U> updates, Function<? super U, ? extends K> lookupKeyFunction, Function<? super U, ? extends K> retainedKeyFunction, BiFunction<? super U, ? super V, ? extends V> updateFunction) Applies ordered updates while retaining a stable key only when an update inserts a new mapping.<U> voidupdateAll(Map<? extends K, ? extends U> updates, BiFunction<? super U, ? super V, ? extends V> updateFunction) Applies independent updates under one cache lock and checks memory pressure once after the batch.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) -
MemoryAwareCacheSupport
public MemoryAwareCacheSupport(long maxWeight, long maxEntryWeight, ToLongBiFunction<? super K, ? super V> weigher, Comparator<? super K> keyComparator, MemoryPressureController memoryPressureController, Duration memoryPressureCheckInterval, MemoryAwareCacheSupport.WeightUnit weightUnit)
-
-
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
-
updateAll
public <U> void updateAll(Map<? extends K, ? extends U> updates, BiFunction<? super U, ? super V, ? extends V> updateFunction) Applies independent updates under one cache lock and checks memory pressure once after the batch.Iteration order, admission, size eviction and manual-removal notifications are identical to applying the updates individually. The batch as a whole is not an atomic transaction.
-
updateAll
public <U> void updateAll(Iterable<? extends U> updates, Function<? super U, ? extends K> keyFunction, BiFunction<? super U, ? super V, ? extends V> updateFunction) Applies ordered updates under one cache lock and checks memory pressure once after the batch. Repeated keys are applied in iteration order. -
updateAll
public <U> void updateAll(Iterable<? extends U> updates, Function<? super U, ? extends K> lookupKeyFunction, Function<? super U, ? extends K> retainedKeyFunction, BiFunction<? super U, ? super V, ? extends V> updateFunction) Applies ordered updates while retaining a stable key only when an update inserts a new mapping. Repeated keys are applied in iteration order. -
get
-
supplyAll
public <U> void supplyAll(Iterable<? extends U> lookups, Function<? super U, ? extends K> keyFunction, BiConsumer<? super U, ? super V> valueConsumer) Supplies present values for an ordered group of lookups under one cache lock. Every hit retains the same LRU and cross-cache access bookkeeping asget(Object). -
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
-