Browse Source

Merge pull request #24774 from abpframework/auto-merge/rel-10-1/4332

Merge branch dev with rel-10.1
pull/24785/head
Ma Liming 1 week ago
committed by GitHub
parent
commit
6ad2d9451a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs
  2. 11
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs
  3. 56
      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;

11
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<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)
});
context.Services.AddEntityCache<Product, ProductCacheItem2, Guid>();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

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

Loading…
Cancel
Save