Class SelectiveCache

java.lang.Object
io.fluxzero.sdk.persisting.caching.SelectiveCache

public class SelectiveCache extends Object
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 Details

    • SelectiveCache

      public SelectiveCache(io.fluxzero.common.caching.Cache nextCache, Predicate<Object> selector)
      Constructs a SelectiveCache with the current DefaultCache as delegate.
      Parameters:
      nextCache - the fallback cache used when selector does not match a value
      selector - a predicate to decide which cache a value should go into
  • Method Details

    • aggregateSelector

      public static Predicate<Object> aggregateSelector(Class<?> type)
      Utility predicate to match aggregates of a given root type. This checks the type of the value inside an Entity, or the declared type if the value is null.
    • put

      public Object put(Object id, Object value)
    • putIfAbsent

      public Object putIfAbsent(Object id, Object value)
    • computeIfAbsent

      public <T> T computeIfAbsent(Object id, Function<? super Object, T> mappingFunction)
    • computeIfPresent

      public <T> T computeIfPresent(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction)
    • compute

      public <T> T compute(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction)
    • get

      public <T> T get(Object id)
    • containsKey

      public boolean containsKey(Object id)
    • remove

      public <T> T remove(Object id)
    • clear

      public void clear()
    • modifyEach

      public <T> void modifyEach(BiFunction<? super Object, ? super T, ? extends T> modifierFunction)
    • 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

      default <T> T getOrDefault(Object arg0, T arg1)
    • isEmpty

      default boolean isEmpty()