Class SelectiveCache
java.lang.Object
io.fluxzero.sdk.persisting.caching.SelectiveCache
A
Cache implementation that partitions values across two internal caches based on a provided
Predicate.
If a value matches the selector, it is stored in the delegate cache; otherwise, it is stored in the
nextCache. This is particularly useful when certain types of objects (e.g., aggregates, projections) require
specialized caching behavior while others do not.
Chaining: SelectiveCache instances can be chained by using another
SelectiveCache as the nextCache. This enables complex multi-level cache routing logic, where each
level applies a different selector to direct objects to a different backing cache.
Example use case: Route aggregates to one cache and all other entities to a fallback cache:
Predicate<Object> aggregateSelector = SelectiveCache.aggregateSelector(MyAggregate.class);
Cache aggregateCache = new AdaptiveObjectCache();
Cache fallbackCache = new DefaultCache();
Cache selectiveCache = new SelectiveCache(aggregateCache, aggregateSelector, fallbackCache);
-
Constructor Summary
ConstructorsConstructorDescriptionSelectiveCache(io.fluxzero.common.caching.Cache nextCache, Predicate<Object> selector) Constructs aSelectiveCachewith the currentDefaultCacheas delegate. -
Method Summary
Modifier and TypeMethodDescriptionaggregateSelector(Class<?> type) Utility predicate to match aggregates of a given root type.voidclear()voidclose()<T> Tcompute(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) <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) putIfAbsent(Object id, Object value) io.fluxzero.common.caching.Cacherebuild()io.fluxzero.common.RegistrationregisterEvictionListener(Consumer<io.fluxzero.common.caching.CacheEviction> listener) <T> Tintsize()
-
Constructor Details
-
SelectiveCache
Constructs aSelectiveCachewith the currentDefaultCacheas delegate.- Parameters:
nextCache- the fallback cache used whenselectordoes not match a valueselector- a predicate to decide which cache a value should go into
-
-
Method Details
-
aggregateSelector
-
put
-
putIfAbsent
-
computeIfAbsent
-
computeIfPresent
public <T> T computeIfPresent(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction) -
compute
-
get
-
containsKey
-
remove
-
clear
public void clear() -
modifyEach
-
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() -
getOrDefault
-
isEmpty
default boolean isEmpty()
-