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)
          Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy.
CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory, LockManager lockManager)
          Initializes a repository with a the given aggregateFactory and a pessimistic locking strategy.
 
Method Summary
protected  void doDeleteWithLock(T aggregate)
          Delegates to EventSourcingRepository.doSaveWithLock(EventSourcedAggregateRoot), as Event Sourcing generally doesn't delete aggregates (not their events).
 T doLoad(Object aggregateIdentifier, Long expectedVersion)
          Perform the actual loading of an aggregate.
 void doSaveWithLock(T aggregate)
          Saves the aggregate and stores it in the cache (if configured) for fast retrieval.
 void setCache(net.sf.jsr107cache.Cache cache)
          Set the cache to use for this repository.
 
Methods inherited from class org.axonframework.eventsourcing.EventSourcingRepository
getAggregateFactory, getTypeIdentifier, setConflictResolver, setEventStore, setEventStreamDecorators, setSnapshotterTrigger, validateOnLoad
 
Methods inherited from class org.axonframework.repository.LockingRepository
add, 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)
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
See Also:
LockingRepository.LockingRepository(Class)

CachingEventSourcingRepository

public CachingEventSourcingRepository(AggregateFactory<T> aggregateFactory,
                                      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
lockManager - The lock manager restricting concurrent access to aggregate instances
See Also:
LockingRepository.LockingRepository(Class)
Method Detail

doSaveWithLock

public void doSaveWithLock(T aggregate)
Saves the aggregate and stores it in the cache (if configured) for fast retrieval. If an exception occurs while saving the aggregate, the related cache entry is invalidated immediately.

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.

Overrides:
doSaveWithLock in class EventSourcingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - the aggregate to save

doDeleteWithLock

protected void doDeleteWithLock(T aggregate)
Description copied from class: EventSourcingRepository
Delegates to EventSourcingRepository.doSaveWithLock(EventSourcedAggregateRoot), as Event Sourcing generally doesn't delete aggregates (not their events).

This method may be safely overridden for special cases that do require deleting an Aggregate's Events.

Overrides:
doDeleteWithLock in class EventSourcingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - the aggregate to delete

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(net.sf.jsr107cache.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-2012. All Rights Reserved.