Class DefaultCache
java.lang.Object
io.fluxcapacitor.javaclient.persisting.caching.DefaultCache
- All Implemented Interfaces:
Cache
,AutoCloseable
Default implementation of the
Cache
interface 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
CacheEviction
listeners
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 interface
class
protected class
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a cache with a default maximum size of 1,000,000 entries and no expiration.DefaultCache
(int maxSize) Constructs a cache with a specified max size and no expiration.DefaultCache
(int maxSize, Duration expiry) Constructs a cache with specified size and expiration.DefaultCache
(int maxSize, Executor evictionNotifier, Duration expiry) Constructs a cache with specified size, executor for eviction notifications and expiration.DefaultCache
(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. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries from the cache.void
close()
Closes the cache and releases all associated resources.<T> T
compute
(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) Returns a synchronized computation that adds, removes, or updates a cache entry.<T> T
computeIfAbsent
(Object id, Function<? super Object, T> mappingFunction) If a value is not already associated with the givenid
, computes and stores one using the given function.<T> T
computeIfPresent
(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) If a value is already associated with the givenid
, computes a new value using the provided function and replaces the old one.boolean
containsKey
(Object id) Checks whether the cache contains an entry for the givenid
.<T> T
Retrieves the value associated with the givenid
, ornull
if not found.<T> void
modifyEach
(BiFunction<? super Object, ? super T, ? extends T> modifierFunction) Applies the given modifier function to all values currently in the cache.protected void
Puts a value in the cache for the givenid
, overwriting any existing value.putIfAbsent
(Object id, Object value) Associates the specified value with the givenid
only if no value is currently associated.protected void
registerEviction
(DefaultCache.CacheReference reference, CacheEviction.Reason reason) registerEvictionListener
(Consumer<CacheEviction> listener) Registers a listener to be notified whenever a cache entry is evicted or removed.<T> T
Removes the entry associated with the givenid
, if present.protected void
int
size()
Returns the number of entries currently stored in the cache.protected <T> T
protected DefaultCache.CacheReference
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.persisting.caching.Cache
getOrDefault, isEmpty
-
Field Details
-
mutexPrecursor
- See Also:
-
-
Constructor Details
-
DefaultCache
public DefaultCache()Constructs a cache with a default maximum size of 1,000,000 entries and no expiration. -
DefaultCache
public DefaultCache(int maxSize) Constructs a cache with a specified max size and no expiration. -
DefaultCache
Constructs a cache with specified size and expiration. Uses a single-threaded executor for eviction notifications. -
DefaultCache
Constructs a cache with specified size, executor for eviction notifications and expiration. -
DefaultCache
public DefaultCache(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.
-
-
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
Description copied from interface:Cache
Applies the given modifier function to all values currently in the cache.This is useful for bulk modifications, e.g. adjusting internal state after a system-wide change.
- Specified by:
modifyEach
in interfaceCache
- Type Parameters:
T
- the expected type of the values- Parameters:
modifierFunction
- the function to apply to each entry
-
put
Description copied from interface:Cache
Puts a value in the cache for the givenid
, overwriting any existing value. -
putIfAbsent
Description copied from interface:Cache
Associates the specified value with the givenid
only if no value is currently associated.- Specified by:
putIfAbsent
in interfaceCache
- Parameters:
id
- the key to check for presencevalue
- the value to associate if absent- Returns:
- the existing value associated with the key, or
null
if the new value was successfully put
-
computeIfAbsent
Description copied from interface:Cache
If a value is not already associated with the givenid
, computes and stores one using the given function.- Specified by:
computeIfAbsent
in interfaceCache
- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to check or computemappingFunction
- the function to compute a value if absent- Returns:
- the current or newly computed value
-
computeIfPresent
public <T> T computeIfPresent(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) Description copied from interface:Cache
If a value is already associated with the givenid
, computes a new value using the provided function and replaces the old one.- Specified by:
computeIfPresent
in interfaceCache
- Type Parameters:
T
- the expected type of the value- Parameters:
id
- the key to compute formappingFunction
- the function to compute a new value from the current one- Returns:
- the newly computed value, or
null
if the mapping function returnednull
-
remove
Description copied from interface:Cache
Removes the entry associated with the givenid
, if present. -
get
Description copied from interface:Cache
Retrieves the value associated with the givenid
, ornull
if not found. -
containsKey
Description copied from interface:Cache
Checks whether the cache contains an entry for the givenid
.- Specified by:
containsKey
in interfaceCache
- Parameters:
id
- the key to check- Returns:
true
if the key exists in the cache,false
otherwise
-
clear
public void clear()Description copied from interface:Cache
Removes all entries from the cache. -
size
public int size()Description copied from interface:Cache
Returns the number of entries currently stored in the cache. -
registerEvictionListener
Description copied from interface:Cache
Registers a listener to be notified whenever a cache entry is evicted or removed.- Specified by:
registerEvictionListener
in interfaceCache
- Parameters:
listener
- a function that consumesCacheEviction
s- Returns:
- a registration that can be used to cancel the listener
-
close
public void close()Description copied from interface:Cache
Closes the cache and releases all associated resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCache
-
wrap
-
unwrap
-
pollReferenceQueue
protected void pollReferenceQueue() -
removeExpiredReferences
protected void removeExpiredReferences() -
registerEviction
-