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


C# PokerMemoryCache.Set方法代码示例

本文整理汇总了C#中MonoTests.Common.PokerMemoryCache.Set方法的典型用法代码示例。如果您正苦于以下问题:C# PokerMemoryCache.Set方法的具体用法?C# PokerMemoryCache.Set怎么用?C# PokerMemoryCache.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MonoTests.Common.PokerMemoryCache的用法示例。


在下文中一共展示了PokerMemoryCache.Set方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetEnumerator

		public void GetEnumerator ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			// This one is a Hashtable enumerator
			IEnumerator enumerator = ((IEnumerable) mc).GetEnumerator ();

			// This one is a Dictionary <string, object> enumerator
			IEnumerator enumerator2 = mc.DoGetEnumerator ();

			Assert.IsNotNull (enumerator, "#A1-1");
			Assert.IsNotNull (enumerator2, "#A1-2");
			Assert.IsTrue (enumerator.GetType () != enumerator2.GetType (), "#A1-3");

			mc.Set ("key1", "value1", null);
			mc.Set ("key2", "value2", null);
			mc.Set ("key3", "value3", null);

			bool expired = false;
			var cip = new CacheItemPolicy ();
			cip.AbsoluteExpiration = DateTime.Now.AddMilliseconds (50);
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				expired = true;
			};

			mc.Set ("key4", "value4", cip);
			Thread.Sleep (100);

			enumerator = ((IEnumerable) mc).GetEnumerator ();
			int count = 0;
			while (enumerator.MoveNext ()) {
				count++;
			}

			Assert.IsFalse (expired, "#A2-1");
			Assert.AreEqual (3, count, "#A2-2");

			expired = false;
			cip = new CacheItemPolicy ();
			cip.AbsoluteExpiration = DateTime.Now.AddMilliseconds (50);
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				expired = true;
			};

			mc.Set ("key5", "value5", cip);
			Thread.Sleep (100);

			enumerator2 = mc.DoGetEnumerator ();
			count = 0;
			while (enumerator2.MoveNext ()) {
				count++;
			}

			Assert.IsFalse (expired, "#A3-1");
			Assert.AreEqual (3, count, "#A3-2");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:56,代码来源:MemoryCacheTest.cs

示例2: GetValues

		public void GetValues ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.GetValues (null);
			}, "#A1-1");

			AssertExtensions.Throws<NotSupportedException> (() => {
				mc.GetValues (new string[] {}, "region");
			}, "#A1-2");

			AssertExtensions.Throws<ArgumentException> (() => {
				mc.GetValues (new string [] { "key", null });
			}, "#A1-3");

			IDictionary<string, object> value = mc.GetValues (new string[] {});
			Assert.IsNull (value, "#A2");

			mc.Set ("key1", "value1", null);
			mc.Set ("key2", "value2", null);
			mc.Set ("key3", "value3", null);

			Assert.IsTrue (mc.Contains ("key1"), "#A3-1");
			Assert.IsTrue (mc.Contains ("key2"), "#A3-2");
			Assert.IsTrue (mc.Contains ("key3"), "#A3-2");

			value = mc.GetValues (new string [] { "key1", "key3" });
			Assert.IsNotNull (value, "#A4-1");
			Assert.AreEqual (2, value.Count, "#A4-2");
			Assert.AreEqual ("value1", value ["key1"], "#A4-3");
			Assert.AreEqual ("value3", value ["key3"], "#A4-4");
			Assert.AreEqual (typeof (Dictionary<string, object>), value.GetType (), "#A4-5");

			// LAMESPEC: MSDN says the number of items in the returned dictionary should be the same as in the 
			// 'keys' collection - this is not the case. The returned dictionary contains only entries for keys
			// that exist in the cache.
			value = mc.GetValues (new string [] { "key1", "key3", "nosuchkey" });
			Assert.IsNotNull (value, "#A5-1");
			Assert.AreEqual (2, value.Count, "#A5-2");
			Assert.AreEqual ("value1", value ["key1"], "#A5-3");
			Assert.AreEqual ("value3", value ["key3"], "#A5-4");
			Assert.IsFalse (value.ContainsKey ("Key1"), "#A5-5");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:44,代码来源:MemoryCacheTest.cs

示例3: Remove

		public void Remove ()
		{
			var mc = new PokerMemoryCache ("MyCache");
		
			AssertExtensions.Throws<NotSupportedException> (() => {
				mc.Remove ("key", "region");
			}, "#A1-1");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Remove (null);
			}, "#A1-2");

			bool callbackInvoked;
			CacheEntryRemovedReason reason = (CacheEntryRemovedReason) 1000;
			var cip = new CacheItemPolicy ();
			cip.Priority = CacheItemPriority.NotRemovable;
			mc.Set ("key2", "value1", cip);
			object value = mc.Remove ("key2");

			Assert.IsNotNull (value, "#B1-1");
			Assert.IsFalse (mc.Contains ("key2"), "#B1-2");

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				callbackInvoked = true;
				reason = args.RemovedReason;
			};

			mc.Set ("key", "value", cip);
			callbackInvoked = false;
			reason = (CacheEntryRemovedReason) 1000;
			value = mc.Remove ("key");
			Assert.IsNotNull (value, "#C1-1");
			Assert.IsTrue (callbackInvoked, "#C1-2");
			Assert.AreEqual (CacheEntryRemovedReason.Removed, reason, "#C1-3");

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				callbackInvoked = true;
				reason = args.RemovedReason;
				throw new ApplicationException ("test");
			};

			mc.Set ("key", "value", cip);
			callbackInvoked = false;
			reason = (CacheEntryRemovedReason) 1000;
			value = mc.Remove ("key");
			Assert.IsNotNull (value, "#C2-1");
			Assert.IsTrue (callbackInvoked, "#C2-2");
			Assert.AreEqual (CacheEntryRemovedReason.Removed, reason, "#C2-3");

			// LAMESPEC: UpdateCallback is not called on remove
			cip = new CacheItemPolicy ();
			cip.UpdateCallback = (CacheEntryUpdateArguments args) => {
				callbackInvoked = true;
				reason = args.RemovedReason;
			};

			mc.Set ("key", "value", cip);
			callbackInvoked = false;
			reason = (CacheEntryRemovedReason) 1000;
			value = mc.Remove ("key");
			Assert.IsNotNull (value, "#D1-1");
			Assert.IsFalse (callbackInvoked, "#D1-2");

			cip = new CacheItemPolicy ();
			cip.UpdateCallback = (CacheEntryUpdateArguments args) => {
				callbackInvoked = true;
				reason = args.RemovedReason;
				throw new ApplicationException ("test");
			};

			mc.Set ("key", "value", cip);
			callbackInvoked = false;
			reason = (CacheEntryRemovedReason) 1000;
			value = mc.Remove ("key");
			Assert.IsNotNull (value, "#D2-1");
			Assert.IsFalse (callbackInvoked, "#D2-2");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:79,代码来源:MemoryCacheTest.cs

示例4: TimedExpiration

		public void TimedExpiration ()
		{
			bool expired = false;
			CacheEntryRemovedReason reason = CacheEntryRemovedReason.CacheSpecificEviction;
			NameValueCollection config;
			int sleepPeriod;
#if !DOTNET
			config = new NameValueCollection ();
			config.Add ("__MonoTimerPeriod", "1");
			sleepPeriod = 1100;
#else
			config = null;
			sleepPeriod = 20100; // 20s is the .NET period - discovered by experimentation
#endif
			var mc = new PokerMemoryCache ("MyCache", config);
			var cip = new CacheItemPolicy ();

			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				expired = true;
				reason = args.RemovedReason;
			};
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (50);
			mc.Set ("key", "value", cip);
			Thread.Sleep (100);

			Assert.IsFalse (expired, "#A1");
			object value = mc.Get ("key");

			Assert.IsNull (value, "#A2-1");
			Assert.IsTrue (expired, "#A2-2");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "A2-3");

			expired = false;
			cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				expired = true;
				reason = args.RemovedReason;
			};
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (50);
			mc.Set ("key", "value", cip);
			Thread.Sleep (sleepPeriod);

			Assert.IsTrue (expired, "#A3-1");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "#A3-2");

			int expiredCount = 0;
			object expiredCountLock = new object ();
			CacheEntryRemovedCallback removedCb = (CacheEntryRemovedArguments args) => {
				lock (expiredCountLock) {
					expiredCount++;
				}
			};

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (20);
			mc.Set ("key1", "value1", cip);

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (200);
			mc.Set ("key2", "value2", cip);

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (600);
			mc.Set ("key3", "value3", cip);

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (sleepPeriod + 100);
			mc.Set ("key4", "value4", cip);
			
			Thread.Sleep (sleepPeriod);
			Assert.AreEqual (3, expiredCount, "#A4");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:76,代码来源:MemoryCacheTest.cs

示例5: Set_String_Object_DateTimeOffset_String

		public void Set_String_Object_DateTimeOffset_String ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			AssertExtensions.Throws<NotSupportedException> (() => {
				mc.Set ("key", "value", DateTimeOffset.MaxValue, "region");
			}, "#A1-1");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Set (null, "value", DateTimeOffset.MaxValue);
			}, "#A1-2");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Set ("key", null, DateTimeOffset.MaxValue);
			}, "#A1-3");
			
			// The entry is never inserted as its expiration date is before now
			mc.Set ("key_A2", "value_A2", DateTimeOffset.MinValue);
			Assert.IsFalse (mc.Contains ("key_A2"), "#A2");

			mc.Calls.Clear ();
			mc.Set ("key", "value", DateTimeOffset.MaxValue);

			Assert.AreEqual (2, mc.Calls.Count, "#A2-1");
			Assert.AreEqual ("Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)", mc.Calls [0], "#A2-2");
			Assert.AreEqual ("Set (string key, object value, CacheItemPolicy policy, string regionName = null)", mc.Calls [1], "#A2-3");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:27,代码来源:MemoryCacheTest.cs

示例6: Set_CacheItem_CacheItemPolicy

		public void Set_CacheItem_CacheItemPolicy ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Set (null, new CacheItemPolicy ());
			}, "#A1-1");

			// Actually thrown from the Set (string, object, CacheItemPolicy, string) overload
			var ci = new CacheItem (null, "value");
			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Set (ci, new CacheItemPolicy ());
			}, "#A1-2");

			ci = new CacheItem ("key", null);
			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Set (ci, new CacheItemPolicy ());
			}, "#A1-3");

			ci = new CacheItem ("key", "value");
			var cip = new CacheItemPolicy ();
			cip.UpdateCallback = (CacheEntryUpdateArguments arguments) => { };
			cip.RemovedCallback = (CacheEntryRemovedArguments arguments) => { };
			AssertExtensions.Throws<ArgumentException> (() => {
				mc.Set (ci, cip);
			}, "#A1-4");

			ci = new CacheItem ("key", "value");
			cip = new CacheItemPolicy ();
			cip.SlidingExpiration = TimeSpan.MinValue;
			AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
				mc.Set (ci, cip);
			}, "#A1-5");

			ci = new CacheItem ("key_A1-6", "value");
			cip = new CacheItemPolicy ();
			cip.SlidingExpiration = TimeSpan.FromTicks (0L);
			mc.Set (ci, cip);
			Assert.IsTrue (mc.Contains ("key_A1-6"), "#A1-6");

			ci = new CacheItem ("key", "value");
			cip = new CacheItemPolicy ();
			cip.SlidingExpiration = TimeSpan.FromDays (500);
			AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
				mc.Set (ci, cip);
			}, "#A1-7");

			ci = new CacheItem ("key_A1-8", "value");
			cip = new CacheItemPolicy ();
			cip.SlidingExpiration = TimeSpan.FromDays (365);
			mc.Set (ci, cip);
			Assert.IsTrue (mc.Contains ("key_A1-8"), "#A1-8");

			ci = new CacheItem ("key", "value");
			cip = new CacheItemPolicy ();
			cip.Priority = (CacheItemPriority) 20;
			AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
				mc.Set (ci, cip);
			}, "#A1-9");

			ci = new CacheItem ("key_A2", "value_A2");
			cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments arguments) => { };
			mc.Set (ci, cip);
			Assert.IsTrue (mc.Contains ("key_A2"), "#A2");

			ci = new CacheItem ("key_A3", "value_A3");
			mc.Set (ci, new CacheItemPolicy ());
			Assert.IsTrue (mc.Contains ("key_A3"), "#A3-1");
			Assert.AreEqual ("value_A3", mc.Get ("key_A3"), "#A3-2");

			// The entry is never inserted as its expiration date is before now
			ci = new CacheItem ("key_A4", "value");
			cip = new CacheItemPolicy ();
			cip.AbsoluteExpiration = DateTimeOffset.MinValue;
			mc.Set (ci, cip);
			Assert.IsFalse (mc.Contains ("key_A4"), "#A4");

			ci = new CacheItem ("key_A5", "value");
			mc.Calls.Clear ();
			mc.Set (ci, new CacheItemPolicy ());

			Assert.AreEqual (2, mc.Calls.Count, "#A5-1");
			Assert.AreEqual ("Set (CacheItem item, CacheItemPolicy policy)", mc.Calls [0], "#A5-2");
			Assert.AreEqual ("Set (string key, object value, CacheItemPolicy policy, string regionName = null)", mc.Calls [1], "#A5-3");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:86,代码来源:MemoryCacheTest.cs

示例7: CreateCacheEntryChangeMonitor

		public void CreateCacheEntryChangeMonitor ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			AssertExtensions.Throws<NotSupportedException> (() => {
				mc.CreateCacheEntryChangeMonitor (new string [] { "key" }, "region");
			}, "#A1-1");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.CreateCacheEntryChangeMonitor (null);
			}, "#A1-2");

			AssertExtensions.Throws<ArgumentException> (() => {
				mc.CreateCacheEntryChangeMonitor (new string [] {});
			}, "#A1-3");

			AssertExtensions.Throws<ArgumentException> (() => {
				mc.CreateCacheEntryChangeMonitor (new string [] { "key", null });
			}, "#A1-4");

			mc.Set ("key1", "value1", ObjectCache.InfiniteAbsoluteExpiration);
			mc.Set ("key2", "value2", ObjectCache.InfiniteAbsoluteExpiration);
			mc.Set ("key3", "value3", ObjectCache.InfiniteAbsoluteExpiration);

			CacheEntryChangeMonitor monitor = mc.CreateCacheEntryChangeMonitor (new string [] { "key1", "key2" });
			Assert.IsNotNull (monitor, "#A2-1");
			Assert.AreEqual ("System.Runtime.Caching.MemoryCacheEntryChangeMonitor", monitor.GetType ().ToString (), "#A2-2");
			Assert.AreEqual (2, monitor.CacheKeys.Count, "#A2-3");
			Assert.AreEqual ("key1", monitor.CacheKeys [0], "#A2-3-1");
			Assert.AreEqual ("key2", monitor.CacheKeys [1], "#A2-3-2");
			Assert.IsNull (monitor.RegionName, "#A2-4");
			// Since this comparison can fail from time to time, leaving it commented out
			//Assert.AreEqual (DateTimeOffset.UtcNow.ToString (), monitor.LastModified.ToString (), "#A2-5");
			Assert.IsFalse (monitor.HasChanged, "#A2-5");

			// The actual unique id is constructed from key names followed by the hex value of ticks of their last modifed time
			Assert.IsFalse (String.IsNullOrEmpty (monitor.UniqueId), "#A2-6");

			// There seems to be a bug in .NET 4.0 regarding the code below. MSDN says that non-existing keys will cause the
			// returned monitor instance to be marked as changed, but instead this exception is thrown:
			//
			// MonoTests.System.Runtime.Caching.MemoryCacheTest.CreateCacheEntryChangeMonitor:
			// System.ArgumentOutOfRangeException : The UTC time represented when the offset is applied must be between year 0 and 10,000.
			// Parameter name: offset
			// 
			// at System.DateTimeOffset.ValidateDate(DateTime dateTime, TimeSpan offset)
			// at System.DateTimeOffset..ctor(DateTime dateTime)
			// at System.Runtime.Caching.MemoryCacheEntryChangeMonitor.InitDisposableMembers(MemoryCache cache)
			// at System.Runtime.Caching.MemoryCache.CreateCacheEntryChangeMonitor(IEnumerable`1 keys, String regionName)
			// at MonoTests.Common.PokerMemoryCache.CreateCacheEntryChangeMonitor(IEnumerable`1 keys, String regionName) in C:\Users\grendel\documents\visual studio 2010\Projects\System.Runtime.Caching.Test\System.Runtime.Caching.Test\Common\PokerMemoryCache.cs:line 113
			// at MonoTests.System.Runtime.Caching.MemoryCacheTest.CreateCacheEntryChangeMonitor() in C:\Users\grendel\documents\visual studio 2010\Projects\System.Runtime.Caching.Test\System.Runtime.Caching.Test\System.Runtime.Caching\MemoryCacheTest.cs:line 275
			//
			// It's probably caused by the code passing a DateTime.MinValue to DateTimeOffset constructor for non-existing entries.
			// Until this (apparent) bug is fixed, Mono is going to implement the buggy behavior.
			//
#if true
			AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
				monitor = mc.CreateCacheEntryChangeMonitor (new string [] { "key1", "doesnotexist" });
			}, "#A3");
#else
			monitor = mc.CreateCacheEntryChangeMonitor (new string [] { "key1", "doesnotexist" });
			Assert.IsNotNull (monitor, "#A3-1");
			Assert.AreEqual ("System.Runtime.Caching.MemoryCacheEntryChangeMonitor", monitor.GetType ().ToString (), "#A3-2");
			Assert.AreEqual (1, monitor.CacheKeys.Count, "#A3-3");
			Assert.AreEqual ("key1", monitor.CacheKeys [0], "#A3-3-1");
			Assert.IsNull (monitor.RegionName, "#A3-4");
			Assert.IsTrue (monitor.HasChanged, "#A3-5");
#endif
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:69,代码来源:MemoryCacheTest.cs

示例8: Contains

		public void Contains ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.Contains (null);
			}, "#A1-1");

			AssertExtensions.Throws<NotSupportedException> (() => {
				mc.Contains ("key", "region");
			}, "#A1-2");

			mc.Set ("key", "value", ObjectCache.InfiniteAbsoluteExpiration);
			Assert.IsTrue (mc.Contains ("key"), "#A2");

			var cip = new CacheItemPolicy ();
			cip.Priority = CacheItemPriority.NotRemovable;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (500);
			mc.Set ("key", "value", cip);
			Assert.IsTrue (mc.Contains ("key"), "#B1-1");
			Thread.Sleep (1000);
			// The call below removes the expired entry and returns false
			Assert.IsFalse (mc.Contains ("key"), "#B1-2");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:24,代码来源:MemoryCacheTest.cs

示例9: ChangeMonitors

		public void ChangeMonitors ()
		{
			bool removed = false;
			var mc = new PokerMemoryCache ("MyCache");
			var cip = new CacheItemPolicy ();
			var monitor = new PokerChangeMonitor ();
			cip.ChangeMonitors.Add (monitor);
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				removed = true;
			};

			mc.Set ("key", "value", cip);
			Assert.AreEqual (0, monitor.Calls.Count, "#A1");

			monitor.SignalChange ();
			Assert.IsTrue (removed, "#A2");

			bool onChangedCalled = false;
			monitor = new PokerChangeMonitor ();
			monitor.NotifyOnChanged ((object state) => {
				onChangedCalled = true;
			});

			cip = new CacheItemPolicy ();
			cip.ChangeMonitors.Add (monitor);

			// Thrown by ChangeMonitor.NotifyOnChanged
			AssertExtensions.Throws<InvalidOperationException> (() => {
				mc.Set ("key1", "value1", cip);
			}, "#A3");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:31,代码来源:MemoryCacheTest.cs

示例10: GetCacheItem

		public void GetCacheItem ()
		{
			var mc = new PokerMemoryCache ("MyCache");

			AssertExtensions.Throws<NotSupportedException> (() => {
				mc.GetCacheItem ("key", "region");
			}, "#A1-1");

			AssertExtensions.Throws<ArgumentNullException> (() => {
				mc.GetCacheItem (null);
			}, "#A1-2");

			CacheItem value;
			mc.Set ("key", "value", null);
			value = mc.GetCacheItem ("key");
			Assert.IsNotNull (value, "#A2-1");
			Assert.AreEqual ("value", value.Value, "#A2-2");
			Assert.AreEqual ("key", value.Key, "#A2-3");

			value = mc.GetCacheItem ("doesnotexist");
			Assert.IsNull (value, "#A3");

			var cip = new CacheItemPolicy ();
			bool callbackInvoked;
			CacheEntryRemovedReason reason = (CacheEntryRemovedReason) 1000;

			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (50);
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				callbackInvoked = true;
				reason = args.RemovedReason;
			};
			mc.Set ("key", "value", cip);
			Thread.Sleep (500);

			callbackInvoked = false;
			reason = (CacheEntryRemovedReason) 1000;
			value = mc.GetCacheItem ("key");
			Assert.IsNull (value, "#B1-1");
			Assert.IsTrue (callbackInvoked, "#B1-2");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "#B1-3");

			cip = new CacheItemPolicy ();
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (50);
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				callbackInvoked = true;
				reason = args.RemovedReason;
				throw new ApplicationException ("test");
			};

			mc.Set ("key", "value", cip);
			Thread.Sleep (500);

			callbackInvoked = false;
			reason = (CacheEntryRemovedReason) 1000;
			value = mc.GetCacheItem ("key");
			Assert.IsNull (value, "#B2-1");
			Assert.IsTrue (callbackInvoked, "#B2-2");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "#B2-3");
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:59,代码来源:MemoryCacheTest.cs

示例11: TimedExpiration

		public void TimedExpiration ()
		{
			bool expired = false;
			CacheEntryRemovedReason reason = CacheEntryRemovedReason.CacheSpecificEviction;
			int sleepPeriod = 1100;

			var mc = new PokerMemoryCache ("MyCache");
			var cip = new CacheItemPolicy ();

			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				expired = true;
				reason = args.RemovedReason;
			};
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (50);
			mc.Set ("key", "value", cip);
			Thread.Sleep (100);

			Assert.IsFalse (expired, "#A1");
			object value = mc.Get ("key");

			Assert.IsNull (value, "#A2-1");
			Assert.IsTrue (expired, "#A2-2");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "A2-3");

			expired = false;
			cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				expired = true;
				reason = args.RemovedReason;
			};
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (50);
			mc.Set ("key", "value", cip);
			Thread.Sleep (sleepPeriod);

			Assert.IsNull (mc.Get ("key"), "#A3-0");
			Assert.IsTrue (expired, "#A3-1");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "#A3-2");

			int expiredCount = 0;
			object expiredCountLock = new object ();
			CacheEntryRemovedCallback removedCb = (CacheEntryRemovedArguments args) => {
				lock (expiredCountLock) {
					expiredCount++;
				}
			};

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (20);
			mc.Set ("key1", "value1", cip);

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (200);
			mc.Set ("key2", "value2", cip);

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (600);
			mc.Set ("key3", "value3", cip);

			cip = new CacheItemPolicy ();
			cip.RemovedCallback = removedCb;
			cip.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds (sleepPeriod + 100);
			mc.Set ("key4", "value4", cip);
			
			Thread.Sleep (sleepPeriod);
			Assert.IsNull (mc.Get ("key1"), "#A4-1");
			Assert.IsNull (mc.Get ("key2"), "#A4-2");
			Assert.IsNull (mc.Get ("key3"), "#A4-3");
			Assert.IsNotNull (mc.Get ("key4"), "#A4-4");
			Assert.AreEqual (3, expiredCount, "#A4");
		}
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:73,代码来源:MemoryCacheTest.cs


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