本文整理汇总了C#中CacheStorage.GetFromCacheOrFetch方法的典型用法代码示例。如果您正苦于以下问题:C# CacheStorage.GetFromCacheOrFetch方法的具体用法?C# CacheStorage.GetFromCacheOrFetch怎么用?C# CacheStorage.GetFromCacheOrFetch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CacheStorage
的用法示例。
在下文中一共展示了CacheStorage.GetFromCacheOrFetch方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunMultipleThreadsWithRandomAccessCalls
public void RunMultipleThreadsWithRandomAccessCalls()
{
var cacheStorage = new CacheStorage<Guid, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(1)));
var threads = new List<Thread>();
for (int i = 0; i < 25; i++)
{
var thread = new Thread(() =>
{
var random = new Random();
for (int j = 0; j < 10000; j++)
{
var randomGuid = _randomGuids[random.Next(0, 9)];
cacheStorage.GetFromCacheOrFetch(randomGuid, () =>
{
var threadId = Thread.CurrentThread.ManagedThreadId;
Log.Info("Key '{0}' is now controlled by thread '{1}'", randomGuid, threadId);
return threadId;
});
ThreadHelper.Sleep(1);
}
});
threads.Add(thread);
thread.Start();
}
while (true)
{
bool anyThreadAlive = false;
foreach (var thread in threads)
{
if (thread.IsAlive)
{
anyThreadAlive = true;
break;
}
}
if (!anyThreadAlive)
{
break;
}
ThreadHelper.Sleep(500);
}
}
示例2: AddsAndExpiresSeveralItems
public void AddsAndExpiresSeveralItems()
{
var cache = new CacheStorage<string, int>();
cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250);
for (int i = 0; i < 5; i++)
{
ThreadHelper.Sleep(1000);
int innerI = i;
var value = cache.GetFromCacheOrFetch("key", () => innerI, expiration: TimeSpan.FromMilliseconds(250));
Assert.AreEqual(i, value);
}
}
示例3: AddsItemToCacheWithOverrideAndReturnsIt
public void AddsItemToCacheWithOverrideAndReturnsIt()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
var value = cache.GetFromCacheOrFetch("1", () => 2, true);
Assert.IsTrue(cache.Contains("1"));
Assert.AreEqual(2, cache["1"]);
Assert.AreEqual(2, value);
}
示例4: ReturnsCachedItem
public void ReturnsCachedItem()
{
var cache = new CacheStorage<string, int>();
cache.Add("1", 1);
var value = cache.GetFromCacheOrFetch("1", () => 2);
Assert.IsTrue(cache.Contains("1"));
Assert.AreEqual(1, cache["1"]);
Assert.AreEqual(1, value);
}
示例5: ThrowsArgumentNullExceptionForNullFunction
public void ThrowsArgumentNullExceptionForNullFunction()
{
var cache = new CacheStorage<string, int>();
ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.GetFromCacheOrFetch("1", null));
}
示例6: AddsAndExpiresSeveralItems
public void AddsAndExpiresSeveralItems()
{
var cache = new CacheStorage<string, int>();
for (int i = 0; i < 5; i++)
{
var value = cache.GetFromCacheOrFetch("key", () => i, expiration: new TimeSpan(0, 0, 1));
Assert.AreEqual(i, value);
ThreadHelper.Sleep(2000);
}
}