diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs index 62d0b5304e..f3af7256b4 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs @@ -16,12 +16,12 @@ public abstract class EntityCacheBase : where TEntityCacheItem : class { protected IReadOnlyRepository Repository { get; } - protected IDistributedCache Cache { get; } + protected IDistributedCache, TKey> Cache { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; } protected EntityCacheBase( IReadOnlyRepository repository, - IDistributedCache cache, + IDistributedCache, TKey> cache, IUnitOfWorkManager unitOfWorkManager) { Repository = repository; @@ -31,7 +31,7 @@ public abstract class EntityCacheBase : public virtual async Task FindAsync(TKey id) { - return await Cache.GetOrAddAsync( + return (await Cache.GetOrAddAsync( id, async () => { @@ -41,7 +41,7 @@ public abstract class EntityCacheBase : } return MapToCacheItem(await Repository.FindAsync(id))!; - }); + }))?.Value; } public virtual async Task GetAsync(TKey id) @@ -56,7 +56,7 @@ public abstract class EntityCacheBase : } return MapToCacheItem(await Repository.GetAsync(id))!; - }))!; + }))!.Value!; } protected virtual bool HasObjectExtensionInfo() @@ -65,7 +65,7 @@ public abstract class EntityCacheBase : ObjectExtensionManager.Instance.GetOrNull(typeof(TEntity)) != null; } - protected abstract TEntityCacheItem? MapToCacheItem(TEntity? entity); + protected abstract EntityCacheItemWrapper? MapToCacheItem(TEntity? entity); public async Task HandleEventAsync(EntityChangedEventData eventData) { diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheItemWrapper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheItemWrapper.cs new file mode 100644 index 0000000000..e1d12692c8 --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheItemWrapper.cs @@ -0,0 +1,12 @@ +namespace Volo.Abp.Domain.Entities.Caching; + +public class EntityCacheItemWrapper + where TEntityCacheItem : class +{ + public TEntityCacheItem? Value { get; set; } + + public EntityCacheItemWrapper(TEntityCacheItem? value) + { + Value = value; + } +} diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs index a37ad49b1e..62b3162cdd 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs @@ -15,7 +15,7 @@ public class EntityCacheWithObjectMapper : public EntityCacheWithObjectMapper( IReadOnlyRepository repository, - IDistributedCache cache, + IDistributedCache, TKey> cache, IUnitOfWorkManager unitOfWorkManager, IObjectMapper objectMapper) : base(repository, cache, unitOfWorkManager) @@ -23,7 +23,7 @@ public class EntityCacheWithObjectMapper : ObjectMapper = objectMapper; } - protected override TEntityCacheItem? MapToCacheItem(TEntity? entity) + protected override EntityCacheItemWrapper? MapToCacheItem(TEntity? entity) { if (entity == null) { @@ -32,9 +32,9 @@ public class EntityCacheWithObjectMapper : if (typeof(TEntity) == typeof(TEntityCacheItem)) { - return entity.As(); + return new EntityCacheItemWrapper(entity.As()); } - return ObjectMapper.Map(entity); + return new EntityCacheItemWrapper(ObjectMapper.Map(entity)); } } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs index f468598652..2471e1057c 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs @@ -12,7 +12,7 @@ public class EntityCacheWithObjectMapperContext repository, - IDistributedCache cache, + IDistributedCache, TKey> cache, IUnitOfWorkManager unitOfWorkManager, IObjectMapper objectMapper)// Intentionally injected with TContext : base(repository, cache, unitOfWorkManager, objectMapper) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs index 125273173e..4738534831 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs @@ -10,14 +10,14 @@ public class EntityCacheWithoutCacheItem : { public EntityCacheWithoutCacheItem( IReadOnlyRepository repository, - IDistributedCache cache, + IDistributedCache, TKey> cache, IUnitOfWorkManager unitOfWorkManager) : base(repository, cache, unitOfWorkManager) { } - protected override TEntity? MapToCacheItem(TEntity? entity) + protected override EntityCacheItemWrapper? MapToCacheItem(TEntity? entity) { - return entity; + return new EntityCacheItemWrapper(entity); } }