Double remove from cache if there is an active UOW.

pull/14055/head
Halil İbrahim Kalkan 3 years ago
parent 4feaeb47f3
commit f96277192b

@ -3,6 +3,7 @@ using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
using Volo.Abp.Uow;
namespace Volo.Abp.Domain.Entities.Caching;
@ -14,13 +15,16 @@ public abstract class EntityCacheBase<TEntity, TEntityCacheItem, TKey> :
{
protected IReadOnlyRepository<TEntity, TKey> Repository { get; }
protected IDistributedCache<TEntityCacheItem, TKey> Cache { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected EntityCacheBase(
IReadOnlyRepository<TEntity, TKey> repository,
IDistributedCache<TEntityCacheItem, TKey> cache)
IDistributedCache<TEntityCacheItem, TKey> cache,
IUnitOfWorkManager unitOfWorkManager)
{
Repository = repository;
Cache = cache;
UnitOfWorkManager = unitOfWorkManager;
}
public virtual async Task<TEntityCacheItem> FindAsync(TKey id)
@ -48,6 +52,19 @@ public abstract class EntityCacheBase<TEntity, TEntityCacheItem, TKey> :
return;
}
/* Why we are using double remove:
* First Cache.RemoveAsync drops the cache item in a unit of work.
* Some other application / thread may read the value from database and put it to the cache again
* before the UOW completes.
* The second Cache.RemoveAsync drops the cache item after the database transaction is complete.
* Only the second Cache.RemoveAsync may not be enough if the application crashes just after the UOW completes.
*/
await Cache.RemoveAsync(eventData.Entity.Id);
if(UnitOfWorkManager.Current != null)
{
await Cache.RemoveAsync(eventData.Entity.Id, considerUow: true);
}
}
}
Loading…
Cancel
Save