本文整理汇总了C#中LruCache.GetOrAdd方法的典型用法代码示例。如果您正苦于以下问题:C# LruCache.GetOrAdd方法的具体用法?C# LruCache.GetOrAdd怎么用?C# LruCache.GetOrAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LruCache
的用法示例。
在下文中一共展示了LruCache.GetOrAdd方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CacheLoaded
static bool CacheLoaded(LruCache<object, object> cache, object key, object value)
{
var loaded = false;
Assert.AreEqual(value, cache.GetOrAdd(key, _ =>
{
loaded = true;
return value;
}));
return loaded;
}
示例2: TestLruCache
public void TestLruCache()
{
var cache = new LruCache<object, object>(5);
Assert.AreEqual(1, cache.GetOrAdd(1, _ => 1));
Assert.AreEqual(2, cache.GetOrAdd(2, _ => 2));
Assert.AreEqual(3, cache.GetOrAdd(3, _ => 3));
Assert.AreEqual(4, cache.GetOrAdd(4, _ => 4));
Assert.AreEqual(5, cache.GetOrAdd(5, _ => 5));
Assert.AreEqual(6, cache.GetOrAdd(6, _ => 6));
Assert.IsTrue(cache.Keys.SequenceEqual(new object[] { 2, 3, 4, 5, 6 }));
Assert.AreEqual(true, CacheLoaded(cache, 1, 1));
Assert.IsTrue(cache.Keys.SequenceEqual(new object[] { 3, 4, 5, 6, 1 }));
Assert.AreEqual(false, CacheLoaded(cache, 1, 1));
Assert.IsTrue(cache.Keys.SequenceEqual(new object[] { 3, 4, 5, 6, 1 }));
Assert.AreEqual(false, CacheLoaded(cache, 4, 4));
Assert.IsTrue(cache.Keys.SequenceEqual(new object[] { 3, 5, 6, 1, 4 }));
Assert.AreEqual(true, CacheLoaded(cache, 2, 2));
Assert.IsTrue(cache.Keys.SequenceEqual(new object[] { 5, 6, 1, 4, 2 }));
}