From 81d34da46cf4a847688cdd69c5a0e8c64a38e835 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 14 Mar 2026 10:51:47 +0800 Subject: [PATCH] Refactor entity cache to use decimal for price and enhance FindManyAsync/GetManyAsync to handle duplicate IDs --- .../framework/infrastructure/entity-cache.md | 6 ++--- .../Entities/Caching/EntityCacheBase.cs | 9 ++++--- .../EntityCacheServiceCollectionExtensions.cs | 8 +++++++ .../Abp/TestApp/Testing/EntityCache_Tests.cs | 24 +++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/docs/en/framework/infrastructure/entity-cache.md b/docs/en/framework/infrastructure/entity-cache.md index 8eace4c6d8..16adef5986 100644 --- a/docs/en/framework/infrastructure/entity-cache.md +++ b/docs/en/framework/infrastructure/entity-cache.md @@ -26,7 +26,7 @@ public class Product : AggregateRoot public string Name { get; set; } public string Description { get; set; } - public float Price { get; set; } + public decimal Price { get; set; } public int StockCount { get; set; } } ``` @@ -72,7 +72,7 @@ public class ProductDto : EntityDto { public string Name { get; set; } public string Description { get; set; } - public float Price { get; set; } + public decimal Price { get; set; } public int StockCount { get; set; } } ``` @@ -189,7 +189,7 @@ public class ProductCacheDto { public Guid Id { get; set; } public string Name { get; set; } - public float Price { get; set; } + public decimal Price { get; set; } } ``` 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 e04e96e798..f2525416d4 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 @@ -49,7 +49,8 @@ public abstract class EntityCacheBase : public virtual async Task> FindManyAsync(IEnumerable ids) { var idArray = ids.ToArray(); - var cacheItems = await GetOrAddManyCacheItemsAsync(idArray); + var distinctIds = idArray.Distinct().ToArray(); + var cacheItems = await GetOrAddManyCacheItemsAsync(distinctIds); #pragma warning disable CS8714 var cacheItemDict = cacheItems.ToDictionary(x => x.Key, x => x.Value); #pragma warning restore CS8714 @@ -77,7 +78,8 @@ public abstract class EntityCacheBase : public virtual async Task> GetManyAsync(IEnumerable ids) { var idArray = ids.ToArray(); - var cacheItems = await GetOrAddManyCacheItemsAsync(idArray); + var distinctIds = idArray.Distinct().ToArray(); + var cacheItems = await GetOrAddManyCacheItemsAsync(distinctIds); #pragma warning disable CS8714 var cacheItemDict = cacheItems.ToDictionary(x => x.Key, x => x.Value); #pragma warning restore CS8714 @@ -109,7 +111,8 @@ public abstract class EntityCacheBase : var missingKeyArray = missingKeys.ToArray(); var entities = await Repository.GetListAsync( - x => missingKeyArray.Contains(x.Id) + x => missingKeyArray.Contains(x.Id), + includeDetails: true ); #pragma warning disable CS8714 var entityDict = entities.ToDictionary(e => e.Id); diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs index b2aa58cc4c..2919b6b0d5 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs @@ -80,6 +80,14 @@ public static class EntityCacheServiceCollectionExtensions options.ConfigureCache>(cacheOptions ?? GetDefaultCacheOptions()); }); + if (typeof(TEntity) == typeof(TEntityCacheItem)) + { + services.Configure(options => + { + options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers().CreateModifyAction(x => x.Id)); + }); + } + return services; } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs index ff0dc41f05..09913caaf2 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs @@ -196,6 +196,30 @@ public abstract class EntityCache_Tests : TestAppTestBase