Class SoftReferenceCache
java.lang.Object
io.fluxzero.sdk.persisting.caching.SoftReferenceCache
- All Implemented Interfaces:
AutoCloseable
Cache implementation using key-level synchronized access and soft references for value storage.
This cache is optimized for concurrent environments and provides built-in support for:
- Automatic eviction based on max size (LRU policy)
- Reference-based eviction when values are no longer strongly reachable (via
SoftReference) - Expiration after a configurable duration
- Eviction notification via registered
CacheEvictionlisteners
The cache ensures thread safety through synchronized access on its internal LinkedHashMap and striped locking
for computations. Equal keys always use the same stripe, preserving atomic updates without allocating or globally
interning a lock value for every cache operation.
Threading: Eviction listeners and expiration polling run on background threads.
The valueMap itself is backed by a synchronized LinkedHashMap with LRU eviction behavior.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceclassprotected class -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a cache with a default maximum size of 1,000,000 entries and no expiration.SoftReferenceCache(int maxSize) Constructs a cache with a specified max size and no expiration.SoftReferenceCache(int maxSize, Duration expiry) Constructs a cache with specified size and expiration.SoftReferenceCache(int maxSize, Executor evictionNotifier, Duration expiry) Constructs a cache with specified size, executor for eviction notifications and expiration.SoftReferenceCache(int maxSize, Executor evictionNotifier, Duration expiry, Duration expiryCheckDelay, boolean softReferences) Constructs a cache with full configuration of size, eviction executor, expiration delay, and expiration check frequency.protectedSoftReferenceCache(int maxSize, Executor evictionNotifier, Duration expiry, Duration expiryCheckDelay, boolean softReferences, Clock clock) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidclose()<T> Tcompute(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) Returns a synchronized computation that adds, removes, or updates a cache entry.<T> TcomputeIfAbsent(Object id, Function<? super Object, T> mappingFunction) <T> TcomputeIfPresent(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) booleancontainsKey(Object id) <T> Tdefault <T> TgetOrDefault(Object arg0, T arg1) default booleanisEmpty()<T> voidmergeAll(Map<?, ? extends T> values, BiFunction<? super T, ? super T, ? extends T> mergeFunction) <T> voidmodifyEach(BiFunction<? super Object, ? super T, ? extends T> modifierFunction) protected voidputIfAbsent(Object id, Object value) io.fluxzero.common.caching.Cacherebuild()protected voidregisterEviction(SoftReferenceCache.CacheReference reference, io.fluxzero.common.caching.CacheEviction.Reason reason) io.fluxzero.common.RegistrationregisterEvictionListener(Consumer<io.fluxzero.common.caching.CacheEviction> listener) <T> Tprotected voidintsize()default <U,T> void supplyAll(Iterable<? extends U> arg0, Function<? super U, ?> arg1, BiConsumer<? super U, ? super T> arg2) protected <T> Tdefault <U,T> void updateAll(Iterable<? extends U> arg0, Function<? super U, ?> arg1, BiFunction<? super U, ? super T, ? extends T> arg2) default <U,T> void updateAll(Iterable<? extends U> arg0, Function<? super U, ?> arg1, Function<? super U, ?> arg2, BiFunction<? super U, ? super T, ? extends T> arg3) <T> voidprotected SoftReferenceCache.CacheReference
-
Constructor Details
-
SoftReferenceCache
public SoftReferenceCache()Constructs a cache with a default maximum size of 1,000,000 entries and no expiration. -
SoftReferenceCache
public SoftReferenceCache(int maxSize) Constructs a cache with a specified max size and no expiration. -
SoftReferenceCache
Constructs a cache with specified size and expiration. Uses a single-threaded executor for eviction notifications. -
SoftReferenceCache
-
SoftReferenceCache
-
SoftReferenceCache
-
-
Method Details
-
compute
Returns a synchronized computation that adds, removes, or updates a cache entry. A fixed hash stripe preserves per-key atomicity without allocating and globally interning a mutex String for every cache operation. -
mergeAll
public <T> void mergeAll(Map<?, ? extends T> values, BiFunction<? super T, ? super T, ? extends T> mergeFunction) -
updateAll
-
modifyEach
-
put
-
putIfAbsent
-
computeIfAbsent
-
computeIfPresent
public <T> T computeIfPresent(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) -
remove
-
get
-
containsKey
-
clear
public void clear() -
size
public int size() -
registerEvictionListener
public io.fluxzero.common.Registration registerEvictionListener(Consumer<io.fluxzero.common.caching.CacheEviction> listener) -
rebuild
public io.fluxzero.common.caching.Cache rebuild() -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
wrap
-
unwrap
-
pollReferenceQueue
protected void pollReferenceQueue() -
removeExpiredReferences
protected void removeExpiredReferences() -
registerEviction
protected void registerEviction(SoftReferenceCache.CacheReference reference, io.fluxzero.common.caching.CacheEviction.Reason reason) -
updateAll
default <U,T> void updateAll(Iterable<? extends U> arg0, Function<? super U, ?> arg1, BiFunction<? super U, ? super T, ? extends T> arg2) -
updateAll
default <U,T> void updateAll(Iterable<? extends U> arg0, Function<? super U, ?> arg1, Function<? super U, ?> arg2, BiFunction<? super U, ? super T, ? extends T> arg3) -
supplyAll
default <U,T> void supplyAll(Iterable<? extends U> arg0, Function<? super U, ?> arg1, BiConsumer<? super U, ? super T> arg2) -
getOrDefault
-
isEmpty
default boolean isEmpty()
-