diff --git a/src/JKang.EventSourcing.Persistence.Caching/CachedAggregateRepository.cs b/src/JKang.EventSourcing.Persistence.Caching/CachedAggregateRepository.cs index 5d6337b..5132766 100644 --- a/src/JKang.EventSourcing.Persistence.Caching/CachedAggregateRepository.cs +++ b/src/JKang.EventSourcing.Persistence.Caching/CachedAggregateRepository.cs @@ -45,17 +45,24 @@ protected override async Task FindAggregateAsync( int version = -1, CancellationToken cancellationToken = default) { - string key = GetCacheKey(id, version); - string serialized = await _cache.GetStringAsync(key, cancellationToken).ConfigureAwait(false); - if (!string.IsNullOrEmpty(serialized)) + try { - return DeserializeAggregate(serialized); + string key = GetCacheKey(id, version); + string serialized = await _cache.GetStringAsync(key, cancellationToken).ConfigureAwait(false); + if (!string.IsNullOrEmpty(serialized)) + { + return DeserializeAggregate(serialized); + } } + catch { } TAggregate aggregate = await base.FindAggregateAsync(id, ignoreSnapshot, version, cancellationToken) .ConfigureAwait(false); - await CacheAsync(aggregate, version, cancellationToken).ConfigureAwait(false); + if (aggregate != null) + { + await CacheAsync(aggregate, version, cancellationToken).ConfigureAwait(false); + } return aggregate; }