class Synapse::EventSourcing::CachingEventSourcingRepository
Implementation of an event sourcing repository that uses a cache to improve performance when loading aggregates
Caching is not compatible with optimistic locking
Note that if an error occurs while saving an aggregate, it will be invalidated from the cache to prevent aggregates being returned from the cache that were not fully persisted to disk.
Attributes
cache[RW]
@return [ActiveSupport::Cache::Store]
Protected Instance Methods
perform_load(aggregate_id, expected_version)
click to toggle source
@raise [AggregateNotFoundError]
If the aggregate with the given identifier could not be found
@raise [AggregateDeletedError]
If the loaded aggregate has been marked as deleted
@raise [ConflictingModificationError]
If the expected version doesn't match the aggregate's actual version
@param [Object] aggregate_id @param [Integer] expected_version @return [AggregateRoot]
Calls superclass method
# File lib/synapse/event_sourcing/caching.rb, line 25 def perform_load(aggregate_id, expected_version) aggregate = @cache.fetch aggregate_id if aggregate.nil? aggregate = super aggregate_id, expected_version elsif aggregate.deleted? raise AggregateDeletedError.new type_identifier, aggregate_id end register_listener CacheClearingUnitOfWorkListener.new aggregate_id, @cache aggregate end
save_aggregate_with_lock(aggregate)
click to toggle source
@param [AggregateRoot] aggregate @return [undefined]
Calls superclass method
# File lib/synapse/event_sourcing/caching.rb, line 41 def save_aggregate_with_lock(aggregate) super aggregate @cache.write aggregate.id, aggregate end