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
Relationshipupdates. - Configurable slow-tracking detection via
slowTrackingThreshold(default 10 seconds).
This caching repository is enabled by default in all Fluxzero applications to support aggregate access
during event processing. If needed, it can be disabled using
FluxzeroBuilder.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 voiddeleteAggregate(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 voidhandleEvents(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 voidprotected voidupdateRelationships(Entity<?> before, Entity<?> after) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AggregateRepository
getLatestAggregateId, load, load, repairRelationships, repairRelationships
-
Field Details
-
slowTrackingThreshold
-
-
Constructor Details
-
CachingAggregateRepository
public CachingAggregateRepository()
-
-
Method Details
-
load
Description copied from interface:AggregateRepositoryLoad an aggregate by its identifier and type.- Specified by:
loadin 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:AggregateRepositoryLoad 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:
loadForin 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:AggregateRepositoryWrap an existing aggregate instance into anEntity, initializing tracking and identity information.- Specified by:
asEntityin interfaceAggregateRepository- Type Parameters:
T- the aggregate type.- Parameters:
entityId- the aggregate instance.- Returns:
- the entity wrapper.
-
repairRelationships
Description copied from interface:AggregateRepositoryRepairs the internal relationship model for a loaded aggregate.This is useful when refactoring entity hierarchies or recovering from inconsistent relationship state.
- Specified by:
repairRelationshipsin interfaceAggregateRepository- Parameters:
aggregate- the aggregate to inspect.- Returns:
- a future that completes when the relationships are updated.
-
getAggregatesFor
Description copied from interface:AggregateRepositoryReturns a map of aggregate IDs and their types that are associated with a given entity ID.- Specified by:
getAggregatesForin interfaceAggregateRepository- Parameters:
entityId- the child or nested entity.- Returns:
- a map of aggregate IDs to class names.
-
deleteAggregate
Description copied from interface:AggregateRepositoryDeletes the persisted state for an aggregate, including its events or document and relationships.- Specified by:
deleteAggregatein 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()
-