From b5cd873526b1d05218c126c21eb46bbf0d58d0d0 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 29 Jan 2026 19:03:05 +0800 Subject: [PATCH 1/2] Use EntityCacheItemWrapper in cache config Resolve #24770 --- .../EntityCacheServiceCollectionExtensions.cs | 6 +-- .../Volo/Abp/TestApp/TestAppModule.cs | 10 +++- .../Abp/TestApp/Testing/EntityCache_Tests.cs | 49 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) 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 81d2993c4f..c35406b45b 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 @@ -20,7 +20,7 @@ public static class EntityCacheServiceCollectionExtensions services.Configure(options => { - options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); + options.ConfigureCache>(cacheOptions ?? GetDefaultCacheOptions()); }); services.Configure(options => @@ -42,7 +42,7 @@ public static class EntityCacheServiceCollectionExtensions services.Configure(options => { - options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); + options.ConfigureCache>(cacheOptions ?? GetDefaultCacheOptions()); }); return services; @@ -59,7 +59,7 @@ public static class EntityCacheServiceCollectionExtensions services.Configure(options => { - options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); + options.ConfigureCache>(cacheOptions ?? GetDefaultCacheOptions()); }); return services; diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs index f30774838e..1be226febb 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs @@ -30,8 +30,14 @@ public class TestAppModule : AbpModule context.Services.AddHttpContextAccessor(); context.Services.Replace(ServiceDescriptor.Singleton()); - context.Services.AddEntityCache(); - context.Services.AddEntityCache(); + context.Services.AddEntityCache(new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(7) + }); + context.Services.AddEntityCache(new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(9) + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) 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 5e321736d4..347d244567 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 @@ -1,5 +1,9 @@ using System; +using System.Reflection; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Shouldly; using Volo.Abp.Caching; using Volo.Abp.Domain.Entities; @@ -116,6 +120,51 @@ public abstract class EntityCache_Tests : TestAppTestBase, Guid>>(); + var productCacheItemCache = GetRequiredService, Guid>>(); + + var productOptions = GetDefaultCachingOptions(productCache); + productOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(7)); + productOptions.SlidingExpiration.ShouldBeNull(); + + var productCacheItemOptions = GetDefaultCachingOptions(productCacheItemCache); + productCacheItemOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(9)); + productCacheItemOptions.SlidingExpiration.ShouldBeNull(); + } + + [Fact] + public void EntityCache_Configured_Options_Should_Be_Applied() + { + // This test verifies that the cache options configured during AddEntityCache are properly applied + // The options are configured in TestAppModule.ConfigureServices + var productCache = GetRequiredService, Guid>>(); + var productCacheItemCache = GetRequiredService, Guid>>(); + + GetDefaultCachingOptions(productCache).AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(7)); + GetDefaultCachingOptions(productCacheItemCache).AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(9)); + } + + private static DistributedCacheEntryOptions GetDefaultCachingOptions(object instance) + { + var internalCacheProperty = instance + .GetType() + .GetProperty("InternalCache", BindingFlags.Instance | BindingFlags.Public); + + if (internalCacheProperty != null) + { + instance = internalCacheProperty.GetValue(instance); + } + + var defaultOptionsField = instance + .GetType() + .GetField("DefaultCacheOptions", BindingFlags.Instance | BindingFlags.NonPublic); + + return (DistributedCacheEntryOptions)defaultOptionsField.GetValue(instance); + } } [Serializable] From dbcd7dbcb9386a4771d9430181b2d9accf5b0605 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 29 Jan 2026 19:37:04 +0800 Subject: [PATCH 2/2] Add ProductCacheItem2 and update entity cache tests --- .../Volo/Abp/TestApp/TestAppModule.cs | 1 + .../Abp/TestApp/Testing/EntityCache_Tests.cs | 35 +++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs index 1be226febb..438bf4b3b9 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs @@ -38,6 +38,7 @@ public class TestAppModule : AbpModule { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(9) }); + context.Services.AddEntityCache(); } public override void OnApplicationInitialization(ApplicationInitializationContext context) 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 347d244567..6ef8da9b7c 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 @@ -2,8 +2,6 @@ using System.Reflection; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Shouldly; using Volo.Abp.Caching; using Volo.Abp.Domain.Entities; @@ -122,30 +120,28 @@ public abstract class EntityCache_Tests : TestAppTestBase, Guid>>(); - var productCacheItemCache = GetRequiredService, Guid>>(); + var productCache = GetRequiredService, Guid>>(); var productOptions = GetDefaultCachingOptions(productCache); - productOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(7)); + productOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(2)); productOptions.SlidingExpiration.ShouldBeNull(); - - var productCacheItemOptions = GetDefaultCachingOptions(productCacheItemCache); - productCacheItemOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(9)); - productCacheItemOptions.SlidingExpiration.ShouldBeNull(); } [Fact] public void EntityCache_Configured_Options_Should_Be_Applied() { - // This test verifies that the cache options configured during AddEntityCache are properly applied - // The options are configured in TestAppModule.ConfigureServices var productCache = GetRequiredService, Guid>>(); var productCacheItemCache = GetRequiredService, Guid>>(); - GetDefaultCachingOptions(productCache).AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(7)); - GetDefaultCachingOptions(productCacheItemCache).AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(9)); + var productOptions = GetDefaultCachingOptions(productCache); + productOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(7)); + productOptions.SlidingExpiration.ShouldBeNull(); + + var productCacheItemOptions = GetDefaultCachingOptions(productCacheItemCache); + productCacheItemOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(9)); + productCacheItemOptions.SlidingExpiration.ShouldBeNull(); } private static DistributedCacheEntryOptions GetDefaultCachingOptions(object instance) @@ -197,3 +193,14 @@ public class ProductCacheItem public decimal Price { get; set; } } + +[Serializable] +[CacheName("ProductCacheItem2")] +public class ProductCacheItem2 +{ + public Guid Id { get; set; } + + public string Name { get; set; } + + public decimal Price { get; set; } +}