Class CachingAggregateRepository
- All Implemented Interfaces:
AggregateRepository
AggregateRepository
that ensures cached aggregates stay in sync with the event
log.
This repository starts an internal TrackingClient
that tails the
global event log. It deserializes received events and applies them to any corresponding aggregate in the cache,
thereby ensuring that all cached aggregates reflect the latest known state.
This design makes it possible to load up-to-date aggregates within event and notification handlers, allowing these types of handlers to rely on the event model as a read model. This enables event-sourced read models without requiring the developer to maintain separate query-oriented state.
Specifically, when an event handler loads an aggregate, the repository first ensures that all events up to and including the event being handled have been processed by the tracker and applied to the cache. This prevents race conditions and stale reads.
If a cache miss occurs, the aggregate is loaded from the delegate repository and cached for future access.
Features
- Transparent event replay on cached aggregates.
- Ensures correct ordering and consistency during event handling via index synchronization.
- Relationship metadata is kept up to date using
Relationship
updates. - Configurable slow-tracking detection via
slowTrackingThreshold
(default 10 seconds).
This caching repository is enabled by default in all Flux Capacitor applications to support aggregate access
during event processing. If needed, it can be disabled using
FluxCapacitorBuilder.disableAutomaticAggregateCaching()
, in which case aggregate state will not be tracked
or kept in sync automatically, and developers are expected to manage consistency manually.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Entity
<T> asEntity
(T entityId) Wrap an existing aggregate instance into anEntity
, initializing tracking and identity information.protected void
deleteAggregate
(Object aggregateId) Deletes the persisted state for an aggregate, including its events or document and relationships.getAggregatesFor
(Object entityId) Returns a map of aggregate IDs and their types that are associated with a given entity ID.protected void
handleEvents
(List<SerializedMessage> messages) <T> Entity
<T> Load an aggregate by its identifier and type.<T> Entity
<T> Load the aggregate that owns the specified entity.repairRelationships
(Entity<?> aggregate) Repairs the internal relationship model for a loaded aggregate.protected void
protected void
updateRelationships
(Entity<?> before, Entity<?> after) 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.repository.AggregateRepository
getLatestAggregateId, load, repairRelationships, repairRelationships
-
Field Details
-
slowTrackingThreshold
-
-
Constructor Details
-
CachingAggregateRepository
public CachingAggregateRepository()
-
-
Method Details
-
load
Description copied from interface:AggregateRepository
Load an aggregate by its identifier and type.- Specified by:
load
in interfaceAggregateRepository
- Type Parameters:
T
- the aggregate type.- Parameters:
aggregateId
- the aggregate identifier.type
- the expected class type.- Returns:
- the loaded aggregate wrapped in an
Entity
.
-
loadFor
Description copied from interface:AggregateRepository
Load the aggregate that owns the specified entity.If no ownership is found in the relationship index, this method may fall back to loading the entity as if it were an aggregate itself.
- Specified by:
loadFor
in interfaceAggregateRepository
- Type Parameters:
T
- the aggregate type.- Parameters:
entityId
- the child or nested entity.defaultType
- fallback type to use when no aggregate mapping is available.- Returns:
- the loaded aggregate as an
Entity
.
-
asEntity
Description copied from interface:AggregateRepository
Wrap an existing aggregate instance into anEntity
, initializing tracking and identity information.- Specified by:
asEntity
in interfaceAggregateRepository
- Type Parameters:
T
- the aggregate type.- Parameters:
entityId
- the aggregate instance.- Returns:
- the entity wrapper.
-
repairRelationships
Description copied from interface:AggregateRepository
Repairs the internal relationship model for a loaded aggregate.This is useful when refactoring entity hierarchies or recovering from inconsistent relationship state.
- Specified by:
repairRelationships
in interfaceAggregateRepository
- Parameters:
aggregate
- the aggregate to inspect.- Returns:
- a future that completes when the relationships are updated.
-
getAggregatesFor
Description copied from interface:AggregateRepository
Returns a map of aggregate IDs and their types that are associated with a given entity ID.- Specified by:
getAggregatesFor
in interfaceAggregateRepository
- Parameters:
entityId
- the child or nested entity.- Returns:
- a map of aggregate IDs to class names.
-
deleteAggregate
Description copied from interface:AggregateRepository
Deletes the persisted state for an aggregate, including its events or document and relationships.- Specified by:
deleteAggregate
in interfaceAggregateRepository
- Parameters:
aggregateId
- the ID of the aggregate to delete.- Returns:
- a future that completes when deletion has been confirmed.
-
handleEvents
-
updateRelationships
-
catchUpIfNeeded
protected void catchUpIfNeeded() -
startTrackerIfNeeded
protected void startTrackerIfNeeded()
-