Пример #1
0
		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");
		}
Пример #2
0
		public void Get ()
		{
			var mc = new PokerMemoryCache ("MyCache");

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

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

			object value;
			mc.Set ("key", "value", null);
			value = mc.Get ("key");
			Assert.IsNotNull (value, "#A2-1");
			Assert.AreEqual ("value", value, "#A2-2");

			value = mc.Get ("nosuchkey");
			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.Get ("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.Get ("key");
			Assert.IsNull (value, "#B2-1");
			Assert.IsTrue (callbackInvoked, "#B2-2");
			Assert.AreEqual (CacheEntryRemovedReason.Expired, reason, "#B2-3");
		}
Пример #3
0
		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");
		}
		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");
		}