本文整理汇总了C#中Cache.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.TryGetValue方法的具体用法?C# Cache.TryGetValue怎么用?C# Cache.TryGetValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cache
的用法示例。
在下文中一共展示了Cache.TryGetValue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConditionalCaching
public void ConditionalCaching()
{
var cache = new Cache<string, int>(2);
int value;
if (!cache.TryGetValue("A", out value))
{
value = 1;
cache.Add("A", value);
}
Assert.AreEqual<int>(1, cache.Count, "The count is incorrect.");
Assert.IsTrue(cache.TryGetValue("A", out value), "The cache did not contain 'A'.");
Assert.AreEqual<int>(1, value, "The cached value is incorrect.");
}
示例2: BasicAdd
public void BasicAdd()
{
var cache = new Cache<ByteArray>(500 * 1024, ba => ba.Length );
var items = new List<KeyValuePair<string, ByteArray>>();
for (int i = 0; i < 100; i++) {
var key = ByteArray.Random(40).ToString();
var val = ByteArray.Random(256);
cache.Set(key, val);
items.Add(new KeyValuePair<string, ByteArray>(key, val));
}
ByteArray oval;
for (int i = 0; i < 100; i++) {
var data = items[i];
Assert.IsTrue(cache.TryGetValue(data.Key, out oval));
Assert.AreEqual(data.Value, oval);
}
Assert.IsFalse(cache.TryGetValue(ByteArray.Random(40).ToString(), out oval));
}
示例3: CheckLRU
public void CheckLRU()
{
int limit = 100 * 256;
var cache = new Cache<ByteArray>(limit, ba => ba.Length);
var items = new List<string>();
for (int i = 0; i < 100; i++) {
var key = ByteArray.Random(40).ToString();
var val = ByteArray.Random(256);
cache.Set(key, val);
items.Add(key);
}
ByteArray output;
// Touch the first 50 items again
for (int i = 0; i < 50; i++) {
Assert.IsTrue(cache.TryGetValue(items[i], out output));
}
// Add 10 more items
for (int i = 0; i < 10; i++) {
var key = ByteArray.Random(40).ToString();
var val = ByteArray.Random(256);
cache.Set(key, val);
items.Add(key);
}
// First 50 items should still be there
for (int i = 0; i < 50; i++) {
Assert.IsTrue(cache.TryGetValue(items[i], out output));
}
// Next 10 items should be evicted
for (int i = 50; i < 60; i++) {
Assert.IsFalse(cache.TryGetValue(items[i], out output));
}
// Next 50 items should still be there
for (int i = 60; i < 110; i++) {
Assert.IsTrue(cache.TryGetValue(items[i], out output));
}
Assert.GreaterOrEqual(limit, cache.CurrentSize);
}
示例4: CheckOverSizeLimit
public void CheckOverSizeLimit()
{
int limit = 100 * 256;
var cache = new Cache<ByteArray>(limit, ba => ba.Length);
var items = new List<string>();
int size = 0;
for (int i = 0; i < 200; i++) {
var key = ByteArray.Random(40).ToString();
var val = ByteArray.Random(256);
cache.Set(key, val);
size += val.Length;
items.Add(key);
}
Assert.GreaterOrEqual(limit, cache.CurrentSize);
ByteArray output;
// First 100 items should have been evicted
for (int i = 0; i < 100; i++) {
Assert.IsFalse(cache.TryGetValue(items[i], out output));
}
// Next 100 items should still be there
for (int i = 100; i < 200; i++) {
Assert.IsTrue(cache.TryGetValue(items[i], out output));
}
}
示例5: UpdateCache
public void UpdateCache()
{
var cache = new Cache<string, int>(2);
cache.Add("A", 1);
int value;
Assert.IsTrue(cache.TryGetValue("A", out value), "The cache did not contain 'A'.");
Assert.AreEqual<int>(1, value, "The cached value is incorrect.");
cache.Add("A", 2);
Assert.IsTrue(cache.TryGetValue("A", out value), "The cache did not contain 'A'.");
Assert.AreEqual<int>(2, value, "The cached value is incorrect.");
}