当前位置: 首页>>代码示例>>C#>>正文


C# Cache.Add方法代码示例

本文整理汇总了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();
				}
			}
		}
开发者ID:jmeckley,项目名称:Enterprise-Library-5.0,代码行数:27,代码来源:CacheExceptionHandlingFixture.cs

示例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));
        }
开发者ID:RejectKid,项目名称:RiotSharp,代码行数:10,代码来源:CacheTest.cs

示例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;
 }
开发者ID:michael085,项目名称:StopGuessing,代码行数:7,代码来源:CacheTest.cs

示例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]);
        }
开发者ID:Zpecter,项目名称:MSDNRetos,代码行数:25,代码来源:UnitTestReto6.cs

示例5: TestByKey

        public void TestByKey()
        {
            var cache = new Cache();
            cache.Add("TestKey", 1);

            Assert.IsTrue(Equals(cache["TestKey"], 1));
        }
开发者ID:dbarrera,项目名称:steag,代码行数:7,代码来源:CacheTest.cs

示例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);
                }
            }
        }
开发者ID:bnantz,项目名称:NCS-V1-1,代码行数:27,代码来源:CacheExceptionHandlingFixture.cs

示例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]);
        }
开发者ID:Zpecter,项目名称:MSDNRetos,代码行数:28,代码来源:UnitTestReto6.cs

示例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);
        }
开发者ID:Zpecter,项目名称:MSDNRetos,代码行数:28,代码来源:UnitTestReto6.cs

示例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));
        }
开发者ID:RejectKid,项目名称:RiotSharp,代码行数:7,代码来源:CacheTest.cs

示例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));
        }
开发者ID:RejectKid,项目名称:RiotSharp,代码行数:7,代码来源:CacheTest.cs

示例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();
				}
			}
		}
开发者ID:jmeckley,项目名称:Enterprise-Library-5.0,代码行数:32,代码来源:CacheExceptionHandlingFixture.cs

示例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'.");
        }
开发者ID:heaths,项目名称:psmsi,代码行数:16,代码来源:CacheTests.cs

示例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"));
 }
开发者ID:rahilkidwai,项目名称:Net.Utility,代码行数:7,代码来源:CacheTests.cs

示例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));
        }
开发者ID:RejectKid,项目名称:RiotSharp,代码行数:9,代码来源:CacheTest.cs

示例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));
        }
开发者ID:RejectKid,项目名称:RiotSharp,代码行数:9,代码来源:CacheTest.cs


注:本文中的Cache.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。