org.axonframework.eventsourcing
Class CachingEventSourcingRepository<T extends EventSourcedAggregateRoot>

java.lang.Object
  extended by org.axonframework.repository.AbstractRepository<T>
      extended by org.axonframework.repository.LockingRepository<T>
          extended by org.axonframework.eventsourcing.EventSourcingRepository<T>
              extended by org.axonframework.eventsourcing.CachingEventSourcingRepository<T>
Type Parameters:
T - The type of aggregate this repository stores
All Implemented Interfaces:
Repository<T>

public class CachingEventSourcingRepository<T extends EventSourcedAggregateRoot>
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. Since caching is not compatible with the optimistic locking strategy, only pessimistic locking is available for this type of repository.

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 Summary
CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory, EventStore eventStore)
          Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy.
CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory, EventStore eventStore, LockManager lockManager)
          Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy.
 
Method Summary
 void add(T aggregate)
          Adds the given aggregate to the repository.
 T doLoad(Object aggregateIdentifier, Long expectedVersion)
          Perform the actual loading of an aggregate.
protected  void postDelete(T aggregate)
          Perform action that needs to be done directly after deleting an aggregate and committing the aggregate's uncommitted events.
protected  void postSave(T aggregate)
          Perform action that needs to be done directly after updating an aggregate and committing the aggregate's uncommitted events.
 void setCache(Cache cache)
          Set the cache to use for this repository.
 
Methods inherited from class org.axonframework.eventsourcing.EventSourcingRepository
doDeleteWithLock, doSaveWithLock, getAggregateFactory, getTypeIdentifier, resolveConflicts, setConflictResolver, setEventStreamDecorators, setSnapshotterTrigger, validateOnLoad
 
Methods inherited from class org.axonframework.repository.LockingRepository
doDelete, doSave, load
 
Methods inherited from class org.axonframework.repository.AbstractRepository
getAggregateType, load, setEventBus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachingEventSourcingRepository

public CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory,
                                      EventStore eventStore)
Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy. Optimistic locking is not compatible with caching.

Parameters:
aggregateFactory - The factory for new aggregate instances
eventStore - The event store that holds the event streams for this repository
See Also:
LockingRepository.LockingRepository(Class)

CachingEventSourcingRepository

public CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory,
                                      EventStore eventStore,
                                      LockManager lockManager)
Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy.

Note that an optimistic locking strategy is not compatible with caching.

Parameters:
aggregateFactory - The factory for new aggregate instances
eventStore - The event store that holds the event streams for this repository
lockManager - The lock manager restricting concurrent access to aggregate instances
See Also:
LockingRepository.LockingRepository(Class)
Method Detail

add

public void add(T aggregate)
Description copied from class: AbstractRepository
Adds the given aggregate to the repository. The version of this aggregate must be null, indicating that it has not been previously persisted.

This method will not force the repository to save the aggregate immediately. Instead, it is registered with the current UnitOfWork. To force storage of an aggregate, commit the current unit of work (CurrentUnitOfWork.commit())

Specified by:
add in interface Repository<T extends EventSourcedAggregateRoot>
Overrides:
add in class LockingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - The aggregate to add to the repository.

postSave

protected void postSave(T aggregate)
Description copied from class: AbstractRepository
Perform action that needs to be done directly after updating an aggregate and committing the aggregate's uncommitted events.

Overrides:
postSave in class AbstractRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - The aggregate instance being saved

postDelete

protected void postDelete(T aggregate)
Description copied from class: AbstractRepository
Perform action that needs to be done directly after deleting an aggregate and committing the aggregate's uncommitted events.

Overrides:
postDelete in class AbstractRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - The aggregate instance being saved

doLoad

public T doLoad(Object 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:
doLoad in class EventSourcingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregateIdentifier - the identifier of the aggregate to load
expectedVersion - The expected version of the aggregate
Returns:
the fully initialized aggregate

setCache

public void setCache(Cache cache)
Set the cache to use for this repository. If a cache is not set, caching is disabled for this implementation.

Parameters:
cache - the cache to use


Copyright © 2010-2016. All Rights Reserved.