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 per-key locking
using intern() on a string prefix plus key combination. While this does introduce some memory overhead due to
string interning, it ensures atomic updates for concurrent access to the same key.
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 -
Field Summary
Fields -
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> 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()protected <T> Tprotected SoftReferenceCache.CacheReference
-
Field Details
-
mutexPrecursor
- See Also:
-
-
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. Internally uses per-keyString.intern()synchronization to prevent race conditions. -
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) -
getOrDefault
-
isEmpty
default boolean isEmpty()
-