本文整理汇总了C#中Microsoft.AspNet.Mvc.TagHelpers.CacheTagHelper类的典型用法代码示例。如果您正苦于以下问题:C# CacheTagHelper类的具体用法?C# CacheTagHelper怎么用?C# CacheTagHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CacheTagHelper类属于Microsoft.AspNet.Mvc.TagHelpers命名空间,在下文中一共展示了CacheTagHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateKey_ReturnsKeyBasedOnTagHelperUniqueId
public void GenerateKey_ReturnsKeyBasedOnTagHelperUniqueId()
{
// Arrange
var id = Guid.NewGuid().ToString();
var tagHelperContext = GetTagHelperContext(id);
var cacheTagHelper = new CacheTagHelper(Mock.Of<IMemoryCache>())
{
ViewContext = GetViewContext()
};
var expected = GetHashedBytes("CacheTagHelper||" + id);
// Act
var key = cacheTagHelper.GenerateKey(tagHelperContext);
// Assert
Assert.Equal(expected, key);
}
示例2: GenerateKey_UsesVaryByPropertyToGenerateKey
public void GenerateKey_UsesVaryByPropertyToGenerateKey(string varyBy)
{
// Arrange
var tagHelperContext = GetTagHelperContext();
var cacheTagHelper = new CacheTagHelper(Mock.Of<IMemoryCache>())
{
ViewContext = GetViewContext(),
VaryBy = varyBy
};
var expected = GetHashedBytes("CacheTagHelper||testid||VaryBy||" + varyBy);
// Act
var key = cacheTagHelper.GenerateKey(tagHelperContext);
// Assert
Assert.Equal(expected, key);
}
示例3: GenerateKey_UsesVaryByCookieName
public void GenerateKey_UsesVaryByCookieName(string varyByCookie, string expected)
{
// Arrange
var tagHelperContext = GetTagHelperContext();
var cacheTagHelper = new CacheTagHelper
{
ViewContext = GetViewContext(),
VaryByCookie = varyByCookie
};
cacheTagHelper.ViewContext.HttpContext.Request.Headers["Cookie"] =
"Cookie0=Cookie0Value;Cookie1=Cookie1Value";
// Act
var key = cacheTagHelper.GenerateKey(tagHelperContext);
// Assert
Assert.Equal(GetHashedBytes(expected), key);
}
示例4: ProcessAsync_UsesExpiresOn_ToExpireCacheEntry
public async Task ProcessAsync_UsesExpiresOn_ToExpireCacheEntry()
{
// Arrange - 1
var currentTime = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
var id = "unique-id";
var childContent1 = "original-child-content";
var clock = new Mock<ISystemClock>();
clock.SetupGet(p => p.UtcNow)
.Returns(() => currentTime);
var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
var tagHelperContext1 = GetTagHelperContext(id);
var tagHelperOutput1 = GetTagHelperOutput(childContent: childContent1);
tagHelperOutput1.PreContent.SetContent("<cache>");
tagHelperOutput1.PostContent.SetContent("</cache>");
var cacheTagHelper1 = new CacheTagHelper(cache, new HtmlTestEncoder())
{
ViewContext = GetViewContext(),
ExpiresOn = currentTime.AddMinutes(5)
};
// Act - 1
await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);
// Assert - 1
Assert.Empty(tagHelperOutput1.PreContent.GetContent());
Assert.Empty(tagHelperOutput1.PostContent.GetContent());
Assert.True(tagHelperOutput1.IsContentModified);
Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());
// Arrange - 2
currentTime = currentTime.AddMinutes(5).AddSeconds(2);
var childContent2 = "different-content";
var tagHelperContext2 = GetTagHelperContext(id);
var tagHelperOutput2 = GetTagHelperOutput(childContent: childContent2);
tagHelperOutput2.PreContent.SetContent("<cache>");
tagHelperOutput2.PostContent.SetContent("</cache>");
var cacheTagHelper2 = new CacheTagHelper(cache, new HtmlTestEncoder())
{
ViewContext = GetViewContext(),
ExpiresOn = currentTime.AddMinutes(5)
};
// Act - 2
await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);
// Assert - 2
Assert.Empty(tagHelperOutput2.PreContent.GetContent());
Assert.Empty(tagHelperOutput2.PostContent.GetContent());
Assert.True(tagHelperOutput2.IsContentModified);
Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
}
示例5: ProcessAsync_ReturnsCachedValue_IfEnabled
public async Task ProcessAsync_ReturnsCachedValue_IfEnabled()
{
// Arrange
var id = "unique-id";
var childContent = "original-child-content";
var cache = new Mock<IMemoryCache>();
cache.CallBase = true;
var value = new DefaultTagHelperContent().SetContent("ok");
cache.Setup(c => c.CreateLinkingScope()).Returns(new Mock<IEntryLink>().Object);
cache.Setup(c => c.Set(
/*key*/ It.IsAny<string>(),
/*value*/ It.IsAny<object>(),
/*options*/ It.IsAny<MemoryCacheEntryOptions>()))
.Returns(value)
.Verifiable();
object cacheResult;
cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult))
.Returns(false);
var tagHelperContext = GetTagHelperContext(id, childContent);
var tagHelperOutput = new TagHelperOutput("cache", new TagHelperAttributeList());
var cacheTagHelper = new CacheTagHelper(cache.Object)
{
ViewContext = GetViewContext(),
Enabled = true
};
// Act
await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);
// Assert
Assert.Empty(tagHelperOutput.PreContent.GetContent());
Assert.Empty(tagHelperOutput.PostContent.GetContent());
Assert.True(tagHelperOutput.IsContentModified);
Assert.Equal(childContent, tagHelperOutput.Content.GetContent());
cache.Verify(c => c.Set(
/*key*/ It.IsAny<string>(),
/*value*/ It.IsAny<object>(),
/*options*/ It.IsAny<MemoryCacheEntryOptions>()),
Times.Once);
}
示例6: UpdateCacheContext_UsesAbsoluteExpirationSpecifiedOnEntryLink
public void UpdateCacheContext_UsesAbsoluteExpirationSpecifiedOnEntryLink()
{
// Arrange
var expiresOn = DateTimeOffset.UtcNow.AddMinutes(7);
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheContext = new Mock<ICacheSetContext>(MockBehavior.Strict);
cacheContext.Setup(c => c.SetAbsoluteExpiration(expiresOn))
.Verifiable();
var cacheTagHelper = new CacheTagHelper
{
MemoryCache = cache
};
var entryLink = new EntryLink();
entryLink.SetAbsoluteExpiration(expiresOn);
// Act
cacheTagHelper.UpdateCacheContext(cacheContext.Object, entryLink);
// Assert
cacheContext.Verify();
}
示例7: ProcessAsync_UsesExpiresSliding_ToExpireCacheEntryWithSlidingExpiration
public async Task ProcessAsync_UsesExpiresSliding_ToExpireCacheEntryWithSlidingExpiration()
{
// Arrange - 1
var currentTime = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
var id = "unique-id";
var childContent1 = "original-child-content";
var clock = new Mock<ISystemClock>();
clock.SetupGet(p => p.UtcNow)
.Returns(() => currentTime);
var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
var tagHelperContext1 = GetTagHelperContext(id, childContent1);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
tagHelperOutput1.PreContent.SetContent("<cache>");
tagHelperOutput1.PostContent.SetContent("</cache>");
var cacheTagHelper1 = new CacheTagHelper(cache)
{
ViewContext = GetViewContext(),
ExpiresSliding = TimeSpan.FromSeconds(30)
};
// Act - 1
await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);
// Assert - 1
Assert.Empty(tagHelperOutput1.PreContent.GetContent());
Assert.Empty(tagHelperOutput1.PostContent.GetContent());
Assert.True(tagHelperOutput1.IsContentModified);
Assert.Equal(childContent1, tagHelperOutput1.Content.GetContent());
// Arrange - 2
currentTime = currentTime.AddSeconds(35);
var childContent2 = "different-content";
var tagHelperContext2 = GetTagHelperContext(id, childContent2);
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
tagHelperOutput2.PreContent.SetContent("<cache>");
tagHelperOutput2.PostContent.SetContent("</cache>");
var cacheTagHelper2 = new CacheTagHelper(cache)
{
ViewContext = GetViewContext(),
ExpiresSliding = TimeSpan.FromSeconds(30)
};
// Act - 2
await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);
// Assert - 2
Assert.Empty(tagHelperOutput2.PreContent.GetContent());
Assert.Empty(tagHelperOutput2.PostContent.GetContent());
Assert.True(tagHelperOutput2.IsContentModified);
Assert.Equal(childContent2, tagHelperOutput2.Content.GetContent());
}
示例8: UpdateCacheEntryOptions_SetsCachePreservationPriority
public void UpdateCacheEntryOptions_SetsCachePreservationPriority()
{
// Arrange
var priority = CacheItemPriority.High;
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheTagHelper = new CacheTagHelper(cache)
{
Priority = priority
};
// Act
var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(new EntryLink());
// Assert
Assert.Equal(priority, cacheEntryOptions.Priority);
}
示例9: UpdateCacheEntryOptions_SetsSlidingExpiration_IfExpiresSlidingIsSet
public void UpdateCacheEntryOptions_SetsSlidingExpiration_IfExpiresSlidingIsSet()
{
// Arrange
var expiresSliding = TimeSpan.FromSeconds(37);
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheTagHelper = new CacheTagHelper(cache)
{
ExpiresSliding = expiresSliding
};
// Act
var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(new EntryLink());
// Assert
Assert.Equal(expiresSliding, cacheEntryOptions.SlidingExpiration);
}
示例10: UpdateCacheEntryOptions_PrefersAbsoluteExpirationSpecifiedOnEntryLinkOverExpiresOn
public void UpdateCacheEntryOptions_PrefersAbsoluteExpirationSpecifiedOnEntryLinkOverExpiresOn()
{
// Arrange
var expiresOn1 = DateTimeOffset.UtcNow.AddDays(12);
var expiresOn2 = DateTimeOffset.UtcNow.AddMinutes(4);
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheTagHelper = new CacheTagHelper(cache)
{
ExpiresOn = expiresOn1
};
var entryLink = new EntryLink();
entryLink.SetAbsoluteExpiration(expiresOn2);
// Act
var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(entryLink);
// Assert
Assert.Equal(expiresOn2, cacheEntryOptions.AbsoluteExpiration);
}
示例11: UpdateCacheEntryOptions_SetsAbsoluteExpiration_IfExpiresOnIsSet
public void UpdateCacheEntryOptions_SetsAbsoluteExpiration_IfExpiresOnIsSet()
{
// Arrange
var expiresOn = DateTimeOffset.UtcNow.AddMinutes(4);
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheTagHelper = new CacheTagHelper(cache)
{
ExpiresOn = expiresOn
};
// Act
var cacheEntryOptions = cacheTagHelper.GetMemoryCacheEntryOptions(new EntryLink());
// Assert
Assert.Equal(expiresOn, cacheEntryOptions.AbsoluteExpiration);
}
示例12: ProcessAsync_ReturnsCachedValue_IfVaryByParamIsUnchanged
public async Task ProcessAsync_ReturnsCachedValue_IfVaryByParamIsUnchanged()
{
// Arrange - 1
var id = "unique-id";
var childContent = "original-child-content";
var cache = new MemoryCache(new MemoryCacheOptions());
var tagHelperContext1 = GetTagHelperContext(id, childContent);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList());
var cacheTagHelper1 = new CacheTagHelper(cache)
{
VaryByQuery = "key1,key2",
ViewContext = GetViewContext(),
};
cacheTagHelper1.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
"?key1=value1&key2=value2");
// Act - 1
await cacheTagHelper1.ProcessAsync(tagHelperContext1, tagHelperOutput1);
// Assert - 1
Assert.Empty(tagHelperOutput1.PreContent.GetContent());
Assert.Empty(tagHelperOutput1.PostContent.GetContent());
Assert.True(tagHelperOutput1.IsContentModified);
Assert.Equal(childContent, tagHelperOutput1.Content.GetContent());
// Arrange - 2
var tagHelperContext2 = GetTagHelperContext(id, "different-content");
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList());
var cacheTagHelper2 = new CacheTagHelper(cache)
{
VaryByQuery = "key1,key2",
ViewContext = GetViewContext(),
};
cacheTagHelper2.ViewContext.HttpContext.Request.QueryString = new Http.QueryString(
"?key1=value1&key2=value2");
// Act - 2
await cacheTagHelper2.ProcessAsync(tagHelperContext2, tagHelperOutput2);
// Assert - 2
Assert.Empty(tagHelperOutput2.PreContent.GetContent());
Assert.Empty(tagHelperOutput2.PostContent.GetContent());
Assert.True(tagHelperOutput2.IsContentModified);
Assert.Equal(childContent, tagHelperOutput2.Content.GetContent());
}
示例13: ProcessAsync_DoesNotCache_IfDisabled
public async Task ProcessAsync_DoesNotCache_IfDisabled()
{
// Arrange
var id = "unique-id";
var childContent = "original-child-content";
var cache = new Mock<IMemoryCache>();
cache.CallBase = true;
var value = new DefaultTagHelperContent().SetContent("ok");
cache.Setup(c => c.Set(
/*key*/ It.IsAny<string>(),
/*value*/ value,
/*optons*/ It.IsAny<MemoryCacheEntryOptions>()))
.Returns(value)
.Verifiable();
object cacheResult;
cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult))
.Returns(false);
var tagHelperContext = GetTagHelperContext(id);
var tagHelperOutput = GetTagHelperOutput(
attributes: new TagHelperAttributeList(),
childContent: childContent);
var cacheTagHelper = new CacheTagHelper(cache.Object, new HtmlTestEncoder())
{
ViewContext = GetViewContext(),
Enabled = false
};
// Act
await cacheTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);
// Assert
Assert.Equal(childContent, tagHelperOutput.Content.GetContent());
cache.Verify(c => c.Set(
/*key*/ It.IsAny<string>(),
/*value*/ It.IsAny<object>(),
/*options*/ It.IsAny<MemoryCacheEntryOptions>()),
Times.Never);
}
示例14: UpdateCacheContext_CopiesTriggersFromEntryLink
public void UpdateCacheContext_CopiesTriggersFromEntryLink()
{
// Arrange
var expiresSliding = TimeSpan.FromSeconds(30);
var expected = new[] { Mock.Of<IExpirationTrigger>(), Mock.Of<IExpirationTrigger>() };
var triggers = new List<IExpirationTrigger>();
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheContext = new Mock<ICacheSetContext>();
cacheContext.Setup(c => c.SetSlidingExpiration(expiresSliding))
.Verifiable();
cacheContext.Setup(c => c.AddExpirationTrigger(It.IsAny<IExpirationTrigger>()))
.Callback<IExpirationTrigger>(triggers.Add)
.Verifiable();
var cacheTagHelper = new CacheTagHelper
{
MemoryCache = cache,
ExpiresSliding = expiresSliding
};
var entryLink = new EntryLink();
entryLink.AddExpirationTriggers(expected);
// Act
cacheTagHelper.UpdateCacheContext(cacheContext.Object, entryLink);
// Assert
cacheContext.Verify();
Assert.Equal(expected, triggers);
}
示例15: UpdateCacheContext_SetsCachePreservationPriority
public void UpdateCacheContext_SetsCachePreservationPriority()
{
// Arrange
var priority = CachePreservationPriority.High;
var cache = new MemoryCache(new MemoryCacheOptions());
var cacheContext = new Mock<ICacheSetContext>();
cacheContext.Setup(c => c.SetPriority(priority))
.Verifiable();
var cacheTagHelper = new CacheTagHelper
{
MemoryCache = cache,
Priority = priority
};
// Act
cacheTagHelper.UpdateCacheContext(cacheContext.Object, new EntryLink());
// Assert
cacheContext.Verify();
}