本文整理汇总了C#中Cache.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Cache.Add方法的具体用法?C# Cache.Add怎么用?C# Cache.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cache
的用法示例。
在下文中一共展示了Cache.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExceptionThrownDuringAddIntoIsolatedStorageAllowsItemToBeReaddedLater
public void ExceptionThrownDuringAddIntoIsolatedStorageAllowsItemToBeReaddedLater()
{
using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo", null))
{
ICachingInstrumentationProvider instrumentationProvider = new NullCachingInstrumentationProvider();
Cache cache = new Cache(backingStore, instrumentationProvider);
try
{
try
{
cache.Add("my", new NonSerializableClass());
Assert.Fail("Should have thrown exception internally to Cache.Add");
}
catch (Exception)
{
cache.Add("my", new SerializableClass());
Assert.IsTrue(cache.Contains("my"));
}
}
finally
{
backingStore.Flush();
}
}
}
示例2: Add_ShouldUpdateIfPresent_Test
public void Add_ShouldUpdateIfPresent_Test()
{
Cache cache = new Cache();
var otherValue = "otherValue";
cache.Add(TestKey, TestValue, new TimeSpan(0, 0, 1));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
cache.Add(TestKey, otherValue, new TimeSpan(0, 0, 1));
Assert.AreEqual(otherValue, cache.Get<string, string>(TestKey));
}
示例3: CreateCacheContainingFirstThousandCountingNumbers
private Cache<int, string> CreateCacheContainingFirstThousandCountingNumbers()
{
Cache<int, string> c = new Cache<int, string>();
foreach (KeyValuePair<int, string> entry in Enumerable.Range(1, 1000).Select(i => new KeyValuePair<int, string>(i, i.ToString())))
c.Add(entry);
return c;
}
示例4: TestCacheNoItemsAccessedBeforeGC
public void TestCacheNoItemsAccessedBeforeGC()
{
Cache cache = new Cache();
for (int i = 0; i < 5; i++) {
cache.Add(i, i.ToString());
}
Assert.AreEqual(5, cache.Count);
Assert.AreEqual(5, cache.ActiveCount);
Assert.AreEqual("0", cache[0]);
Assert.AreEqual("1", cache[1]);
Assert.AreEqual("2", cache[2]);
Assert.AreEqual("3", cache[3]);
Assert.AreEqual("4", cache[4]);
GC.Collect();
Assert.AreEqual(5, cache.Count);
Assert.AreEqual(0, cache.ActiveCount);
Assert.IsNull(cache[0]);
Assert.IsNull(cache[1]);
Assert.IsNull(cache[2]);
Assert.IsNull(cache[3]);
Assert.IsNull(cache[4]);
}
示例5: TestByKey
public void TestByKey()
{
var cache = new Cache();
cache.Add("TestKey", 1);
Assert.IsTrue(Equals(cache["TestKey"], 1));
}
示例6: ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails
public void ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails()
{
TestConfigurationContext context = new TestConfigurationContext();
using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo"))
{
CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));
Cache cache = new Cache(backingStore, scavengingPolicy);
cache.Initialize(this);
cache.Add("my", new SerializableClass());
try
{
cache.Add("my", new NonSerializableClass());
Assert.Fail("Should have thrown exception internally to Cache.Add");
}
catch (Exception)
{
Assert.IsFalse(cache.Contains("my"));
Assert.AreEqual(0, backingStore.Count);
Hashtable isolatedStorageContents = backingStore.Load();
Assert.AreEqual(0, isolatedStorageContents.Count);
}
}
}
示例7: TestCacheSomeItemsAccessedJustBeforeGC
public void TestCacheSomeItemsAccessedJustBeforeGC()
{
Cache cache = new Cache();
for (int i = 0; i < 5; i++) {
cache.Add(i, new byte[1024]);
}
var value1 = cache[1];
var value3 = cache[3];
Assert.AreEqual(5, cache.Count);
Assert.AreEqual(5, cache.ActiveCount);
Assert.IsNotNull(cache[0]);
Assert.IsNotNull(cache[1]);
Assert.IsNotNull(cache[2]);
Assert.IsNotNull(cache[3]);
Assert.IsNotNull(cache[4]);
GC.Collect();
Assert.AreEqual(5, cache.Count);
Assert.AreEqual(2, cache.ActiveCount);
Assert.IsNull(cache[0]);
Assert.IsNotNull(cache[1]);
Assert.IsNull(cache[2]);
Assert.IsNotNull(cache[3]);
Assert.IsNull(cache[4]);
}
示例8: TestCacheAllItemsAccessedJustBeforeGC
public void TestCacheAllItemsAccessedJustBeforeGC()
{
Cache cache = new Cache();
for (int i = 0; i < 4; i++) {
cache.Add(i, new DummyClass { Name = i.ToString(), Value = new byte[1024] });
}
var value0 = cache[0];
var value1 = cache[1];
var value2 = cache[2];
var value3 = cache[3];
Assert.AreEqual(4, cache.Count);
Assert.AreEqual(4, cache.ActiveCount);
Assert.AreEqual("0", (cache[0] as DummyClass).Name);
Assert.AreEqual("1", (cache[1] as DummyClass).Name);
Assert.AreEqual("2", (cache[2] as DummyClass).Name);
Assert.AreEqual("3", (cache[3] as DummyClass).Name);
GC.Collect();
Assert.AreEqual(4, cache.Count);
Assert.AreEqual(4, cache.ActiveCount);
Assert.AreEqual("0", (cache[0] as DummyClass).Name);
Assert.AreEqual("1", (cache[1] as DummyClass).Name);
Assert.AreEqual("2", (cache[2] as DummyClass).Name);
Assert.AreEqual("3", (cache[3] as DummyClass).Name);
}
示例9: AddGet_DateTime_ShouldAdd_Test
public void AddGet_DateTime_ShouldAdd_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, DateTime.Now + new TimeSpan(0, 5, 0));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
}
示例10: AddGet_TimeSpan_ShouldAddToTheCache_Test
public void AddGet_TimeSpan_ShouldAddToTheCache_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, new TimeSpan(0, 5, 0));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
}
示例11: ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails
public void ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails()
{
using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo", null))
{
ICachingInstrumentationProvider instrumentationProvider = new NullCachingInstrumentationProvider();
Cache cache = new Cache(backingStore, instrumentationProvider);
cache.Add("my", new SerializableClass());
try
{
try
{
cache.Add("my", new NonSerializableClass());
Assert.Fail("Should have thrown exception internally to Cache.Add");
}
catch (Exception)
{
Assert.IsFalse(cache.Contains("my"));
Assert.AreEqual(0, backingStore.Count);
Hashtable isolatedStorageContents = backingStore.Load();
Assert.AreEqual(0, isolatedStorageContents.Count);
}
}
finally
{
backingStore.Flush();
}
}
}
示例12: BasicCaching
public void BasicCaching()
{
var cache = new Cache<string, int>(2);
cache.Add("A", 1);
cache.Add("B", 2);
Assert.AreEqual<int>(2, cache.Count, "The count is incorrect.");
cache.Add("C", 3);
Assert.AreEqual<int>(2, cache.Count, "The count is incorrect.");
var keys = new List<string>(cache.Keys);
CollectionAssert.DoesNotContain(keys, "A", "The cache contained 'A'.");
CollectionAssert.Contains(keys, "B", "The cache did not contain 'B'.");
CollectionAssert.Contains(keys, "C", "The cache did not contain 'C'.");
}
示例13: Cache_Get_Item_Key_Should_Return_Item
public void Cache_Get_Item_Key_Should_Return_Item()
{
Cache<string, int> cache = new Cache<string, int>();
int EXPECTED = 1;
cache.Add("1", 1);
Assert.AreEqual(EXPECTED, cache.Get("1"));
}
示例14: Remove_ShouldDoNothingIfNull_Test
public void Remove_ShouldDoNothingIfNull_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, new TimeSpan(0, 0, 1));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
cache.Remove<string>(null);
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
}
示例15: AddGet_DateTime_ShouldAddAndExpire_Test
public void AddGet_DateTime_ShouldAddAndExpire_Test()
{
Cache cache = new Cache();
cache.Add(TestKey, TestValue, DateTime.Now + new TimeSpan(0, 0, 1));
Assert.AreEqual(TestValue, cache.Get<string, string>(TestKey));
Thread.Sleep(2000);
Assert.IsNull(cache.Get<string, string>(TestKey));
}