本文整理汇总了C#中CacheManager.GetOrAdd方法的典型用法代码示例。如果您正苦于以下问题:C# CacheManager.GetOrAdd方法的具体用法?C# CacheManager.GetOrAdd怎么用?C# CacheManager.GetOrAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CacheManager
的用法示例。
在下文中一共展示了CacheManager.GetOrAdd方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOrAddValueFactoryTest
public void GetOrAddValueFactoryTest()
{
var cacheManager = new CacheManager();
string key = "key" + DateTime.Now.Ticks;
object value = "value" + DateTime.Now.Ticks;
bool valueCalled = false;
Func<string, object> valueFactory = k => { valueCalled = true; return value; };
object result = cacheManager.GetOrAdd(key, valueFactory);
Assert.IsNotNull(result);
Assert.IsTrue(valueCalled);
Assert.AreEqual(value, result);
// value factory 2 should not be called
object value2 = "value2" + DateTime.Now.Ticks;
bool value2Called = false;
Func<string, object> valueFactory2 = k => { value2Called = true; return value2; };
object result2 = cacheManager.GetOrAdd(key, valueFactory2);
Assert.IsNotNull(result2);
Assert.IsFalse(value2Called);
// result should still = first call
Assert.AreEqual(value, result2);
}