Class SelectiveCache

java.lang.Object
io.fluxzero.sdk.persisting.caching.SelectiveCache
All Implemented Interfaces:
Cache

public class SelectiveCache extends Object implements Cache
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 DefaultCache();
Cache fallbackCache = new DefaultCache();
Cache selectiveCache = new SelectiveCache(aggregateCache, aggregateSelector, fallbackCache);
  • Constructor Summary

    Constructors
    Constructor
    Description
    SelectiveCache(Cache nextCache, Predicate<Object> selector)
    Constructs a SelectiveCache with a default in-memory delegate cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    Utility predicate to match aggregates of a given root type.
    void
    Removes all entries from the cache.
    void
    Closes the cache and releases all associated resources.
    <T> T
    compute(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction)
    Computes and stores a new value for the given id using the provided function.
    <T> T
    computeIfAbsent(Object id, Function<? super Object, T> mappingFunction)
    If a value is not already associated with the given id, 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 given id, computes a new value using the provided function and replaces the old one.
    boolean
    Checks whether the cache contains an entry for the given id.
    <T> T
    get(Object id)
    Retrieves the value associated with the given id, or null 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.
    put(Object id, Object value)
    Puts a value in the cache for the given id, overwriting any existing value.
    Associates the specified value with the given id only if no value is currently associated.
    Returns a fresh cache instance with the same configuration and no stored entries.
    io.fluxzero.common.Registration
    Registers a listener to be notified whenever a cache entry is evicted or removed.
    <T> T
    Removes the entry associated with the given id, if present.
    int
    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, wait

    Methods inherited from interface Cache

    getOrDefault, isEmpty
  • Constructor Details

    • SelectiveCache

      public SelectiveCache(Cache nextCache, Predicate<Object> selector)
      Constructs a SelectiveCache with a default in-memory delegate cache.
      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)
      Description copied from interface: Cache
      Puts a value in the cache for the given id, overwriting any existing value.
      Specified by:
      put in interface Cache
      Parameters:
      id - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      the previous value associated with the id, or null if none
    • putIfAbsent

      public Object putIfAbsent(Object id, Object value)
      Description copied from interface: Cache
      Associates the specified value with the given id only if no value is currently associated.
      Specified by:
      putIfAbsent in interface Cache
      Parameters:
      id - the key to check for presence
      value - the value to associate if absent
      Returns:
      the existing value associated with the key, or null if the new value was successfully put
    • computeIfAbsent

      public <T> T computeIfAbsent(Object id, Function<? super Object, T> mappingFunction)
      Description copied from interface: Cache
      If a value is not already associated with the given id, computes and stores one using the given function.
      Specified by:
      computeIfAbsent in interface Cache
      Type Parameters:
      T - the expected type of the value
      Parameters:
      id - the key to check or compute
      mappingFunction - 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 given id, computes a new value using the provided function and replaces the old one.
      Specified by:
      computeIfPresent in interface Cache
      Type Parameters:
      T - the expected type of the value
      Parameters:
      id - the key to compute for
      mappingFunction - the function to compute a new value from the current one
      Returns:
      the newly computed value, or null if the mapping function returned null
    • compute

      public <T> T compute(Object id, BiFunction<? super Object, ? super T, ? extends T> mappingFunction)
      Description copied from interface: Cache
      Computes and stores a new value for the given id using the provided function.

      The previous value (if any) is provided to the function. The result is stored in the cache.

      Specified by:
      compute in interface Cache
      Type Parameters:
      T - the expected type of the value
      Parameters:
      id - the key to compute for
      mappingFunction - the function to compute a new value
      Returns:
      the newly computed value, or null if the mapping function returned null
    • get

      public <T> T get(Object id)
      Description copied from interface: Cache
      Retrieves the value associated with the given id, or null if not found.
      Specified by:
      get in interface Cache
      Type Parameters:
      T - the expected type of the value
      Parameters:
      id - the key to retrieve
      Returns:
      the cached value, or null if absent
    • containsKey

      public boolean containsKey(Object id)
      Description copied from interface: Cache
      Checks whether the cache contains an entry for the given id.
      Specified by:
      containsKey in interface Cache
      Parameters:
      id - the key to check
      Returns:
      true if the key exists in the cache, false otherwise
    • remove

      public <T> T remove(Object id)
      Description copied from interface: Cache
      Removes the entry associated with the given id, if present.
      Specified by:
      remove in interface Cache
      Type Parameters:
      T - the expected type of the removed value
      Parameters:
      id - the key to remove
      Returns:
      the removed value, or null if no value was associated with the key
    • clear

      public void clear()
      Description copied from interface: Cache
      Removes all entries from the cache.
      Specified by:
      clear in interface Cache
    • modifyEach

      public <T> void modifyEach(BiFunction<? super Object, ? super T, ? extends T> modifierFunction)
      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 interface Cache
      Type Parameters:
      T - the expected type of the values
      Parameters:
      modifierFunction - the function to apply to each entry
    • size

      public int size()
      Description copied from interface: Cache
      Returns the number of entries currently stored in the cache.
      Specified by:
      size in interface Cache
      Returns:
      the number of entries
    • registerEvictionListener

      public io.fluxzero.common.Registration registerEvictionListener(Consumer<CacheEviction> listener)
      Description copied from interface: Cache
      Registers a listener to be notified whenever a cache entry is evicted or removed.
      Specified by:
      registerEvictionListener in interface Cache
      Parameters:
      listener - a function that consumes CacheEvictions
      Returns:
      a registration that can be used to cancel the listener
    • rebuild

      public Cache rebuild()
      Description copied from interface: Cache
      Returns a fresh cache instance with the same configuration and no stored entries.
      Specified by:
      rebuild in interface Cache
    • close

      public void close()
      Description copied from interface: Cache
      Closes the cache and releases all associated resources.
      Specified by:
      close in interface Cache