Class CachingSagaStore<T>

java.lang.Object
org.axonframework.modelling.saga.repository.CachingSagaStore<T>
Type Parameters:
T - The saga type
All Implemented Interfaces:
SagaStore<T>

public class CachingSagaStore<T> extends Object implements SagaStore<T>
Saga Repository implementation that adds caching behavior to the repository it wraps. Both associations and sagas are cached, making loading them faster. Commits and adds are always delegated to the wrapped repository. Loads are only delegated if the cache does not contain the necessary entries.

Updating associations involves a read and write, which are performed atomically. Therefore, it is unsafe to add or remove specific associations outside this instance. Obviously, clearing and evictions are safe.

Since:
2.0
Author:
Allard Buijze
  • Constructor Details

  • Method Details

    • builder

      public static <T> CachingSagaStore.Builder<T> builder()
      Instantiate a Builder to be able to create a CachingSagaStore.

      The delegateSagaStore of type SagaStore, the associationsCache and sagaCache (both of type Cache) are hard requirements and as such should be provided.

      Type Parameters:
      T - a generic specifying the Saga type contained in this SagaRepository implementation
      Returns:
      a Builder to be able to create a CachingSagaStore
    • findSagas

      public Set<String> findSagas(Class<? extends T> sagaType, AssociationValue associationValue)
      Description copied from interface: SagaStore
      Returns identifiers of saga instances of the given sagaType that have been associated with the given associationValue.
      Specified by:
      findSagas in interface SagaStore<T>
      Parameters:
      sagaType - The type of the returned sagas
      associationValue - The value that the returned sagas must be associated with
      Returns:
      A set of identifiers of sagas having the correct type and association value
    • loadSaga

      public <S extends T> SagaStore.Entry<S> loadSaga(Class<S> sagaType, String sagaIdentifier)
      Description copied from interface: SagaStore
      Loads a known saga SagaStore.Entry instance with given sagaType and unique sagaIdentifier.

      Due to the concurrent nature of Sagas, it is not unlikely for a Saga to have ceased to exist after it has been found based on associations. Therefore, a repository should return null in case a Saga doesn't exists, as opposed to throwing an exception.

      Specified by:
      loadSaga in interface SagaStore<T>
      Parameters:
      sagaType - The type of the returned saga entry
      sagaIdentifier - The unique identifier of the returned saga entry
      Returns:
      The saga entry, or null if no such saga exists
    • insertSaga

      public void insertSaga(Class<? extends T> sagaType, String sagaIdentifier, T saga, Set<AssociationValue> associationValues)
      Description copied from interface: SagaStore
      Adds a new Saga and its initial association values to the store. The tracking token of the event last handled by the Saga (usually the event that started the Saga) is also passed as a parameter. Note that the given token may be null if the Saga is not tracking the event store.
      Specified by:
      insertSaga in interface SagaStore<T>
      Parameters:
      sagaType - The type of the Saga
      sagaIdentifier - The identifier of the Saga
      saga - The Saga instance
      associationValues - The initial association values of the Saga
    • deleteSaga

      public void deleteSaga(Class<? extends T> sagaType, String sagaIdentifier, Set<AssociationValue> associationValues)
      Description copied from interface: SagaStore
      Deletes a Saga with given sagaType and sagaIdentifier and all its associations. For convenience all known association values are passed along as well, which has the advantage that the saga store is not required to keep an index of association value to saga identifier.
      Specified by:
      deleteSaga in interface SagaStore<T>
      Parameters:
      sagaType - The type of saga to delete
      sagaIdentifier - The identifier of the saga to delete
      associationValues - The known associations of the saga
    • addCachedAssociations

      protected void addCachedAssociations(Iterable<AssociationValue> associationValues, String sagaIdentifier, Class<?> sagaType)
      Registers the associations of a saga with given sagaIdentifier and given sagaType with the associations cache.
      Parameters:
      associationValues - the association values of the saga
      sagaIdentifier - the identifier of the saga
      sagaType - the type of the saga
    • updateSaga

      public void updateSaga(Class<? extends T> sagaType, String sagaIdentifier, T saga, AssociationValues associationValues)
      Description copied from interface: SagaStore
      Updates a given Saga after its state was modified. The tracking token of the event last handled by the Saga is also passed as a parameter. Note that the given token may be null if the Saga is not tracking the event store.
      Specified by:
      updateSaga in interface SagaStore<T>
      Parameters:
      sagaType - The type of the Saga
      sagaIdentifier - The identifier of the Saga
      saga - The Saga instance
      associationValues - The initial association values of the Saga