Browse Source

Use EntityCacheItemWrapper in cache config

Resolve #24770
pull/24772/head
maliming 1 week ago
parent
commit
b5cd873526
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 6
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs
  2. 10
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs
  3. 49
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs

6
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs

@ -20,7 +20,7 @@ public static class EntityCacheServiceCollectionExtensions
services.Configure<AbpDistributedCacheOptions>(options =>
{
options.ConfigureCache<TEntity>(cacheOptions ?? GetDefaultCacheOptions());
options.ConfigureCache<EntityCacheItemWrapper<TEntity>>(cacheOptions ?? GetDefaultCacheOptions());
});
services.Configure<AbpSystemTextJsonSerializerModifiersOptions>(options =>
@ -42,7 +42,7 @@ public static class EntityCacheServiceCollectionExtensions
services.Configure<AbpDistributedCacheOptions>(options =>
{
options.ConfigureCache<TEntityCacheItem>(cacheOptions ?? GetDefaultCacheOptions());
options.ConfigureCache<EntityCacheItemWrapper<TEntityCacheItem>>(cacheOptions ?? GetDefaultCacheOptions());
});
return services;
@ -59,7 +59,7 @@ public static class EntityCacheServiceCollectionExtensions
services.Configure<AbpDistributedCacheOptions>(options =>
{
options.ConfigureCache<TEntityCacheItem>(cacheOptions ?? GetDefaultCacheOptions());
options.ConfigureCache<EntityCacheItemWrapper<TEntityCacheItem>>(cacheOptions ?? GetDefaultCacheOptions());
});
return services;

10
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<IDistributedCache, TestMemoryDistributedCache>());
context.Services.AddEntityCache<Product, Guid>();
context.Services.AddEntityCache<Product, ProductCacheItem, Guid>();
context.Services.AddEntityCache<Product, Guid>(new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(7)
});
context.Services.AddEntityCache<Product, ProductCacheItem, Guid>(new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(9)
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

49
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<TStartupModule> : TestAppTestBase<TStart
productCacheItem.Name.ShouldBe("Product2");
productCacheItem.Price.ShouldBe(decimal.Zero);
}
[Fact]
public void EntityCache_DefaultOptions_Should_Be_Applied()
{
var productCache = GetRequiredService<IDistributedCache<EntityCacheItemWrapper<Product>, Guid>>();
var productCacheItemCache = GetRequiredService<IDistributedCache<EntityCacheItemWrapper<ProductCacheItem>, 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<IDistributedCache<EntityCacheItemWrapper<Product>, Guid>>();
var productCacheItemCache = GetRequiredService<IDistributedCache<EntityCacheItemWrapper<ProductCacheItem>, 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]

Loading…
Cancel
Save