本文整理汇总了C#中Cache.GetOrAdd方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.GetOrAdd方法的具体用法?C# Cache.GetOrAdd怎么用?C# Cache.GetOrAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cache
的用法示例。
在下文中一共展示了Cache.GetOrAdd方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CacheBeforeExpiration
public void CacheBeforeExpiration()
{
var target = new Cache<string>();
target.GetOrAdd("k", () => "v1", () => DateTime.UtcNow.AddMinutes(1));
var actual = target.GetOrAdd("k", () => { throw new InvalidOperationException(); }, () => { throw new InvalidOperationException(); });
Assert.AreEqual("v1", actual);
}
示例2: CacheAfterExpiration
public void CacheAfterExpiration()
{
var target = new Cache<string>();
target.GetOrAdd("k", () => "v1", () => DateTime.UtcNow.AddMilliseconds(100));
System.Threading.Thread.Sleep(1000);
var actual = target.GetOrAdd("k", () => "v2", () => DateTime.UtcNow.AddMilliseconds(100));
Assert.AreEqual("v2", actual);
}
示例3: GetOrAddWithCacheHitTest
public void GetOrAddWithCacheHitTest()
{
// Arrange
// Arrange
var invocationCount = 0;
Func<int, string> valueFactory = k =>
{
invocationCount++;
return Convert.ToString(k);
};
var cache = new Cache<int, string>();
// Act
cache.GetOrAdd(1, valueFactory);
var value = cache.GetOrAdd(1, valueFactory);
// Assert
Assert.That(value, Is.EqualTo("1"));
Assert.That(invocationCount, Is.EqualTo(1));
}
示例4: CacheTrivial
public void CacheTrivial()
{
var target = new Cache<string>();
var actual = target.GetOrAdd("k", () => "v1", () => DateTime.UtcNow);
Assert.AreEqual("v1", actual);
}