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..438bf4b3b9 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,15 @@ 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) + }); + 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 5e321736d4..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 @@ -1,5 +1,7 @@ using System; +using System.Reflection; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; using Shouldly; using Volo.Abp.Caching; using Volo.Abp.Domain.Entities; @@ -116,6 +118,49 @@ public abstract class EntityCache_Tests : TestAppTestBase, Guid>>(); + + var productOptions = GetDefaultCachingOptions(productCache); + productOptions.AbsoluteExpirationRelativeToNow.ShouldBe(TimeSpan.FromMinutes(2)); + productOptions.SlidingExpiration.ShouldBeNull(); + } + + [Fact] + public void EntityCache_Configured_Options_Should_Be_Applied() + { + var productCache = GetRequiredService, 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(); + } + + 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] @@ -148,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; } +}