本文整理汇总了C#中CacheItem.CacheOutOfDate方法的典型用法代码示例。如果您正苦于以下问题:C# CacheItem.CacheOutOfDate方法的具体用法?C# CacheItem.CacheOutOfDate怎么用?C# CacheItem.CacheOutOfDate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CacheItem
的用法示例。
在下文中一共展示了CacheItem.CacheOutOfDate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCacheOutOfDate
public void TestCacheOutOfDate()
{
var cacheItem = new CacheItem<string>(() => TestString, MinuteTimeSpan);
// Call Value to update cache.
Assert.AreEqual(TestString, cacheItem.Value, "Cache has the wrong value.");
Assert.IsFalse(cacheItem.CacheOutOfDate(), "Cache should not be out of date yet.");
Thread.Sleep(MinuteTimeSpan);
Assert.IsTrue(cacheItem.CacheOutOfDate(), "Cache should be out of date after the timespan.");
// Now we call Value to update cache again.
Assert.AreEqual(TestString, cacheItem.Value, "Cache has the wrong value.");
Assert.IsFalse(cacheItem.CacheOutOfDate(), "Cache should not be out of date yet.");
}
示例2: TestGetValueWithUpdateFunction
public void TestGetValueWithUpdateFunction()
{
var cacheItem = new CacheItem<string>(cacheLifetime: MinuteTimeSpan);
// Without update function, we will get a null or empty string.
Assert.IsTrue(string.IsNullOrEmpty(cacheItem.Value), "Cache should return default value without update function.");
string value = cacheItem.GetValueWithUpdateFunction(() => TestString);
Assert.AreEqual(value, TestString, "GetValueWithUpdateFunction returns the incorrect value.");
Assert.IsFalse(cacheItem.CacheOutOfDate(), "Cache should not be out of date yet.");
}
示例3: TestGetValueWithoutUpdateFunction
public void TestGetValueWithoutUpdateFunction()
{
string before = "before";
string after = "after";
string changingString = before;
// This function returns changingString so if we change changingString, the function will return a different value.
Func<string> testFunction = () =>
{
return changingString;
};
var cacheItem = new CacheItem<string>(testFunction, cacheLifetime: MinuteTimeSpan);
Assert.AreEqual(cacheItem.Value, before, "Cache has the wrong value.");
changingString = after;
// Wait for a minute before calling Value again so the item will be updated.
Thread.Sleep(MinuteTimeSpan);
Assert.IsTrue(cacheItem.CacheOutOfDate(), "Cache should be out of date after the timespan.");
Assert.AreEqual(cacheItem.GetLastValueWithoutUpdate(), before, "GetLastValueWithoutUpdate should return value before the update.");
Assert.AreEqual(cacheItem.Value, after, "Cache has the wrong value.");
}