mirror of https://github.com/abpframework/abp.git
8 changed files with 202 additions and 9 deletions
@ -0,0 +1,25 @@ |
|||||
|
using Microsoft.Extensions.Caching.Distributed; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Volo.Abp.Caching |
||||
|
{ |
||||
|
public class CacheOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Global Cache entry options.
|
||||
|
/// </summary>
|
||||
|
public DistributedCacheEntryOptions GlobalCacheEntryOptions { get; set; } |
||||
|
/// <summary>
|
||||
|
/// List of all cache configurators.
|
||||
|
/// (func argument:Name of cache)
|
||||
|
/// </summary>
|
||||
|
public List<Func<string, DistributedCacheEntryOptions>> CacheConfigurators { get; set; } //TODO list item use a configurator interface instead?
|
||||
|
public CacheOptions() |
||||
|
{ |
||||
|
CacheConfigurators = new List<Func<string, DistributedCacheEntryOptions>>(); |
||||
|
GlobalCacheEntryOptions = new DistributedCacheEntryOptions(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,10 +1,30 @@ |
|||||
using Volo.Abp.Modularity; |
using Microsoft.Extensions.Caching.Distributed; |
||||
|
using System; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
namespace Volo.Abp.Caching |
namespace Volo.Abp.Caching |
||||
{ |
{ |
||||
[DependsOn(typeof(AbpCachingModule))] |
[DependsOn(typeof(AbpCachingModule))] |
||||
public class AbpCachingTestModule : AbpModule |
public class AbpCachingTestModule : AbpModule |
||||
{ |
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
Configure<CacheOptions>(option => |
||||
|
{ |
||||
|
option.CacheConfigurators.Add(cacheName => |
||||
|
{ |
||||
|
if (cacheName == typeof(Sail.Testing.Caching.PersonCacheItem).FullName) |
||||
|
{ |
||||
|
return new DistributedCacheEntryOptions() |
||||
|
{ |
||||
|
AbsoluteExpiration = DateTime.Parse("2099-01-01 12:00:00") |
||||
|
}; |
||||
|
} |
||||
|
return null; |
||||
|
}); |
||||
|
|
||||
|
option.GlobalCacheEntryOptions.SetSlidingExpiration(TimeSpan.FromMinutes(20)); |
||||
|
}); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,51 @@ |
|||||
|
using Microsoft.Extensions.Caching.Distributed; |
||||
|
using Shouldly; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Reflection; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Caching |
||||
|
{ |
||||
|
public class DistributedCache_ConfigureOptions_Test : AbpIntegratedTest<AbpCachingTestModule> |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task Configure_CacheOptions() |
||||
|
{ |
||||
|
var personCache = GetRequiredService<IDistributedCache<Sail.Testing.Caching.PersonCacheItem>>(); |
||||
|
|
||||
|
var cacheKey = Guid.NewGuid().ToString(); |
||||
|
//Get (not exists yet)
|
||||
|
var cacheItem = await personCache.GetAsync(cacheKey); |
||||
|
|
||||
|
cacheItem.ShouldBeNull(); |
||||
|
|
||||
|
GetDefaultCachingOptions(personCache).SlidingExpiration.ShouldBeNull(); |
||||
|
|
||||
|
GetDefaultCachingOptions(personCache).AbsoluteExpiration.ShouldBe(new DateTime(2099, 1, 1, 12, 0, 0)); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Default_CacheOptions_Should_Be_20_Mins() |
||||
|
{ |
||||
|
var personCache = GetRequiredService<IDistributedCache<PersonCacheItem>>(); |
||||
|
|
||||
|
var cacheKey = Guid.NewGuid().ToString(); |
||||
|
|
||||
|
//Get (not exists yet)
|
||||
|
var cacheItem = await personCache.GetAsync(cacheKey); |
||||
|
cacheItem.ShouldBeNull(); |
||||
|
|
||||
|
GetDefaultCachingOptions(personCache).SlidingExpiration.ShouldBe(TimeSpan.FromMinutes(20)); |
||||
|
|
||||
|
} |
||||
|
private static DistributedCacheEntryOptions GetDefaultCachingOptions(object instance) |
||||
|
{ |
||||
|
var defaultOptionsField = instance.GetType().GetTypeInfo().GetField("DefaultCacheOptions", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
return (DistributedCacheEntryOptions)defaultOptionsField.GetValue(instance); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue