Add a default sliding expiration of 30 seconds on Memory and Distributed Cache Tag Helpers.

This commit is contained in:
Javier Calvarro Nelson 2017-07-03 13:17:59 -07:00
parent 057a853de7
commit c50f55d1de
5 changed files with 64 additions and 3 deletions

View File

@ -153,19 +153,23 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
// Internal for unit testing
internal MemoryCacheEntryOptions GetMemoryCacheEntryOptions()
{
var hasEvictionCriteria = false;
var options = new MemoryCacheEntryOptions();
if (ExpiresOn != null)
{
hasEvictionCriteria = true;
options.SetAbsoluteExpiration(ExpiresOn.Value);
}
if (ExpiresAfter != null)
{
hasEvictionCriteria = true;
options.SetAbsoluteExpiration(ExpiresAfter.Value);
}
if (ExpiresSliding != null)
{
hasEvictionCriteria = true;
options.SetSlidingExpiration(ExpiresSliding.Value);
}
@ -174,6 +178,11 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
options.SetPriority(Priority.Value);
}
if (!hasEvictionCriteria)
{
options.SetSlidingExpiration(DefaultExpiration);
}
return options;
}

View File

@ -25,6 +25,13 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
private const string ExpiresSlidingAttributeName = "expires-sliding";
private const string EnabledAttributeName = "enabled";
/// <summary>
/// The default duration, from the time the cache entry was added, when it should be evicted.
/// This default duration will only be used if no other expiration criteria is specified.
/// The default expiration time is a sliding expiration of 30 seconds.
/// </summary>
public static readonly TimeSpan DefaultExpiration = TimeSpan.FromSeconds(30);
/// <summary>
/// Creates a new <see cref="CacheTagHelperBase"/>.
/// </summary>
@ -109,6 +116,5 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
/// </summary>
[HtmlAttributeName(EnabledAttributeName)]
public bool Enabled { get; set; } = true;
}
}

View File

@ -85,24 +85,32 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
// Internal for unit testing
internal DistributedCacheEntryOptions GetDistributedCacheEntryOptions()
{
var hasEvictionCriteria = false;
var options = new DistributedCacheEntryOptions();
if (ExpiresOn != null)
{
hasEvictionCriteria = true;
options.SetAbsoluteExpiration(ExpiresOn.Value);
}
if (ExpiresAfter != null)
{
hasEvictionCriteria = true;
options.SetAbsoluteExpiration(ExpiresAfter.Value);
}
if (ExpiresSliding != null)
{
hasEvictionCriteria = true;
options.SetSlidingExpiration(ExpiresSliding.Value);
}
if (!hasEvictionCriteria)
{
options.SetSlidingExpiration(DefaultExpiration);
}
return options;
}
}
}
}

View File

@ -210,6 +210,21 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
Assert.Equal(expiresOn, cacheEntryOptions.AbsoluteExpiration);
}
[Fact]
public void UpdateCacheEntryOptions_DefaultsTo30SecondsSliding_IfNoEvictionCriteriaIsProvided()
{
// Arrange
var slidingExpiresIn = TimeSpan.FromSeconds(30);
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheTagHelper = new CacheTagHelper(cache, new HtmlTestEncoder());
// Act
var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions();
// Assert
Assert.Equal(slidingExpiresIn, cacheEntryOptions.SlidingExpiration);
}
[Fact]
public void UpdateCacheEntryOptions_SetsAbsoluteExpiration_IfExpiresAfterIsSet()
{

View File

@ -271,6 +271,29 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
Assert.Equal(expiresOn, cacheEntryOptions.AbsoluteExpiration);
}
[Fact]
public void UpdateCacheEntryOptions_DefaultsTo30SecondsSliding_IfNoEvictionCriteriaIsProvided()
{
// Arrange
var slidingExpiresIn = TimeSpan.FromSeconds(30);
var storage = GetStorage();
var service = new DistributedCacheTagHelperService(
storage,
Mock.Of<IDistributedCacheTagHelperFormatter>(),
new HtmlTestEncoder(),
NullLoggerFactory.Instance
);
var cacheTagHelper = new DistributedCacheTagHelper(
service,
new HtmlTestEncoder());
// Act
var cacheEntryOptions = cacheTagHelper.GetDistributedCacheEntryOptions();
// Assert
Assert.Equal(slidingExpiresIn, cacheEntryOptions.SlidingExpiration);
}
[Fact]
public void UpdateCacheEntryOptions_SetsAbsoluteExpiration_IfExpiresAfterIsSet()
{