diff --git a/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelper.cs b/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelper.cs
index 1ca3ca9bc1..81f5933e54 100644
--- a/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelper.cs
+++ b/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelper.cs
@@ -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;
}
diff --git a/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelperBase.cs b/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelperBase.cs
index f147c38e89..ca7dfdcb70 100644
--- a/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelperBase.cs
+++ b/src/Microsoft.AspNetCore.Mvc.TagHelpers/CacheTagHelperBase.cs
@@ -25,6 +25,13 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
private const string ExpiresSlidingAttributeName = "expires-sliding";
private const string EnabledAttributeName = "enabled";
+ ///
+ /// 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.
+ ///
+ public static readonly TimeSpan DefaultExpiration = TimeSpan.FromSeconds(30);
+
///
/// Creates a new .
///
@@ -109,6 +116,5 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
///
[HtmlAttributeName(EnabledAttributeName)]
public bool Enabled { get; set; } = true;
-
}
}
\ No newline at end of file
diff --git a/src/Microsoft.AspNetCore.Mvc.TagHelpers/DistributedCacheTagHelper.cs b/src/Microsoft.AspNetCore.Mvc.TagHelpers/DistributedCacheTagHelper.cs
index ede06d1bfc..7256f950d9 100644
--- a/src/Microsoft.AspNetCore.Mvc.TagHelpers/DistributedCacheTagHelper.cs
+++ b/src/Microsoft.AspNetCore.Mvc.TagHelpers/DistributedCacheTagHelper.cs
@@ -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;
}
-
}
-}
+}
\ No newline at end of file
diff --git a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/CacheTagHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/CacheTagHelperTest.cs
index 30ea407095..d694059b11 100644
--- a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/CacheTagHelperTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/CacheTagHelperTest.cs
@@ -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()
{
diff --git a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/DistributedCacheTagHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/DistributedCacheTagHelperTest.cs
index 249afe7474..eba589903f 100644
--- a/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/DistributedCacheTagHelperTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.TagHelpers.Test/DistributedCacheTagHelperTest.cs
@@ -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(),
+ 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()
{