Class CachingEventSourcingRepository<T>

Type Parameters:
T - The type of aggregate this repository stores
All Implemented Interfaces:
ScopeAware, Repository<T>

public class CachingEventSourcingRepository<T> extends EventSourcingRepository<T>
Implementation of the event sourcing repository that uses a cache to improve loading performance. The cache removes the need to read all events from disk, at the cost of memory usage.

Note that an entry of a cached aggregate is immediately invalidated when an error occurs while saving that aggregate. This is done to prevent the cache from returning aggregates that may not have fully persisted to disk.

Since:
0.3
Author:
Allard Buijze
  • Constructor Details

  • Method Details

    • validateOnLoad

      protected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion)
      Description copied from class: AbstractRepository
      Checks the aggregate for concurrent changes. Throws a ConflictingModificationException when conflicting changes have been detected.

      This implementation throws a ConflictingAggregateVersionException if the expected version is not null and the version number of the aggregate does not match the expected version

      Overrides:
      validateOnLoad in class EventSourcingRepository<T>
      Parameters:
      aggregate - The loaded aggregate
      expectedVersion - The expected version of the aggregate
    • doSaveWithLock

      protected void doSaveWithLock(EventSourcedAggregate<T> aggregate)
      Description copied from class: LockingRepository
      Perform the actual saving of the aggregate. All necessary locks have been verified.
      Overrides:
      doSaveWithLock in class EventSourcingRepository<T>
      Parameters:
      aggregate - the aggregate to store
    • doDeleteWithLock

      protected void doDeleteWithLock(EventSourcedAggregate<T> aggregate)
      Description copied from class: LockingRepository
      Perform the actual deleting of the aggregate. All necessary locks have been verified.
      Overrides:
      doDeleteWithLock in class EventSourcingRepository<T>
      Parameters:
      aggregate - the aggregate to delete
    • doLoadWithLock

      protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion)
      Perform the actual loading of an aggregate. The necessary locks have been obtained. If the aggregate is available in the cache, it is returned from there. Otherwise the underlying persistence logic is called to retrieve the aggregate.
      Overrides:
      doLoadWithLock in class EventSourcingRepository<T>
      Parameters:
      aggregateIdentifier - the identifier of the aggregate to load
      expectedVersion - The expected version of the aggregate
      Returns:
      the fully initialized aggregate