Class DefaultCache
java.lang.Object
io.fluxzero.sdk.persisting.caching.DefaultCache
- All Implemented Interfaces:
Cache, AutoCloseable
Default aggregate cache facade that selects the current SDK cache implementation from Fluxzero cache properties.
Existing applications without a defaults version or explicit MODE_PROPERTY keep the legacy
SoftReferenceCache. Newer applications that use Fluxzero defaults >= 2026.05.25, or set
fluxzero.cache.mode = adaptive, use AdaptiveObjectCache.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final LocalDateFluxzero defaults version from whichDefaultCacheusesAdaptiveObjectCacheby default.static final StringProperty that configures the maximum entry count forAdaptiveObjectCachewhen selected byDefaultCache.static final StringMODE_PROPERTYvalue that selectsAdaptiveObjectCache.static final StringProperty that selects the cache implementation used byDefaultCache.static final StringMODE_PROPERTYvalue that selectsSoftReferenceCache. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a cache using the active Fluxzero property source, or the default property source when no Fluxzero instance is active.DefaultCache(int maxSize) Constructs a cache with a specified max size for the selected implementation.DefaultCache(int maxSize, Duration expiry) Constructs a cache with specified size and expiration for the selected implementation.DefaultCache(PropertySource propertySource) Constructs a cache using the given property source for cache mode and defaults-version selection.DefaultCache(PropertySource propertySource, int maxSize) Constructs a cache using the given property source for cache mode and defaults-version selection, with a custom max size for the selected implementation. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from the cache.voidclose()Closes the cache and releases all associated resources.<T> Tcompute(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) Computes and stores a new value for the givenidusing the provided function.<T> TcomputeIfAbsent(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> TcomputeIfPresent(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.booleancontainsKey(Object id) Checks whether the cache contains an entry for the givenid.delegate()Returns the selected cache implementation.<T> TRetrieves the value associated with the givenid, ornullif not found.<T> voidmodifyEach(BiFunction<? super Object, ? super T, ? extends T> modifierFunction) Applies the given modifier function to all values currently in the cache.Puts a value in the cache for the givenid, overwriting any existing value.putIfAbsent(Object id, Object value) Associates the specified value with the givenidonly if no value is currently associated.rebuild()Returns a fresh cache instance with the same configuration and no stored entries.registerEvictionListener(Consumer<CacheEviction> listener) Registers a listener to be notified whenever a cache entry is evicted or removed.<T> TRemoves the entry associated with the givenid, if present.intsize()Returns the number of entries currently stored in the cache.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Cache
getOrDefault, isEmpty
-
Field Details
-
ADAPTIVE_DEFAULTS_VERSION
Fluxzero defaults version from whichDefaultCacheusesAdaptiveObjectCacheby default. -
MODE_PROPERTY
Property that selects the cache implementation used byDefaultCache.- See Also:
-
MODE_SOFT_REFERENCE
MODE_PROPERTYvalue that selectsSoftReferenceCache.- See Also:
-
MODE_ADAPTIVE
MODE_PROPERTYvalue that selectsAdaptiveObjectCache.- See Also:
-
MAX_SIZE_PROPERTY
Property that configures the maximum entry count forAdaptiveObjectCachewhen selected byDefaultCache.- See Also:
-
-
Constructor Details
-
DefaultCache
public DefaultCache()Constructs a cache using the active Fluxzero property source, or the default property source when no Fluxzero instance is active. -
DefaultCache
Constructs a cache using the given property source for cache mode and defaults-version selection. -
DefaultCache
Constructs a cache using the given property source for cache mode and defaults-version selection, with a custom max size for the selected implementation. -
DefaultCache
public DefaultCache(int maxSize) Constructs a cache with a specified max size for the selected implementation. -
DefaultCache
Constructs a cache with specified size and expiration for the selected implementation.
-
-
Method Details
-
delegate
Returns the selected cache implementation. -
put
Description copied from interface:CachePuts a value in the cache for the givenid, overwriting any existing value. -
putIfAbsent
Description copied from interface:CacheAssociates the specified value with the givenidonly if no value is currently associated.- Specified by:
putIfAbsentin interfaceCache- Parameters:
id- the key to check for presencevalue- the value to associate if absent- Returns:
- the existing value associated with the key, or
nullif the new value was successfully put
-
computeIfAbsent
Description copied from interface:CacheIf a value is not already associated with the givenid, computes and stores one using the given function.- Specified by:
computeIfAbsentin 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:CacheIf a value is already associated with the givenid, computes a new value using the provided function and replaces the old one.- Specified by:
computeIfPresentin 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
nullif the mapping function returnednull
-
compute
Description copied from interface:CacheComputes and stores a new value for the givenidusing the provided function.The previous value (if any) is provided to the function. The result is stored in the cache.
-
modifyEach
Description copied from interface:CacheApplies 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:
modifyEachin interfaceCache- Type Parameters:
T- the expected type of the values- Parameters:
modifierFunction- the function to apply to each entry
-
get
Description copied from interface:CacheRetrieves the value associated with the givenid, ornullif not found. -
containsKey
Description copied from interface:CacheChecks whether the cache contains an entry for the givenid.- Specified by:
containsKeyin interfaceCache- Parameters:
id- the key to check- Returns:
trueif the key exists in the cache,falseotherwise
-
remove
Description copied from interface:CacheRemoves the entry associated with the givenid, if present. -
clear
-
size
-
registerEvictionListener
Description copied from interface:CacheRegisters a listener to be notified whenever a cache entry is evicted or removed.- Specified by:
registerEvictionListenerin interfaceCache- Parameters:
listener- a function that consumesCacheEvictions- Returns:
- a registration that can be used to cancel the listener
-
rebuild
-
close
public void close()Description copied from interface:CacheCloses the cache and releases all associated resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCache
-