public void PolicyManager() { CachePolicyManager.Reset(); var dsc = new DictionarySetCache <int, string>((data) => new KeyValuePair <int, string>[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2") }); Assert.IsNotNull(dsc.PolicyKey); Assert.AreEqual(0, dsc.Count); Assert.IsTrue(dsc.ContainsKey(1)); Assert.IsTrue(dsc.ContainsKey(2)); Assert.IsFalse(dsc.ContainsKey(3)); Assert.AreEqual(2, dsc.Count); var policy = new DailyCachePolicy(); CachePolicyManager.Set(dsc.PolicyKey, policy); var policy2 = dsc.GetPolicy(); Assert.IsNotNull(policy2); Assert.AreSame(policy, policy2); var pa = CachePolicyManager.GetPolicies(); Assert.AreEqual(1, pa.Length); CachePolicyManager.ForceFlush(); Assert.AreEqual(0, dsc.Count); Assert.IsTrue(dsc.ContainsKey(1)); Assert.IsTrue(dsc.ContainsKey(2)); Assert.IsFalse(dsc.ContainsKey(3)); Assert.AreEqual(2, dsc.Count); }
public void Setup() { var now = DateTime.Now; var dcp = new DailyCachePolicy { Duration = new TimeSpan(1, 0, 0) }; dcp.Reset(); var dt = now.Date.AddHours(now.Hour + 1); Assert.AreEqual(dt, dcp.Expiry); now = DateTime.Now; dcp = new DailyCachePolicy { Duration = new TimeSpan(4, 0, 0) }; dcp.Reset(); dt = now.Date.AddHours(now.Hour + 4 - (now.Hour % 4)); Assert.AreEqual(dt, dcp.Expiry); now = DateTime.Now; dcp = new DailyCachePolicy { Duration = new TimeSpan(24, 0, 0) }; dcp.Reset(); Assert.AreEqual(now.Date.AddDays(1), dcp.Expiry); }
public void Setup_WithRandomizer() { var now = DateTime.Now; var dcp = new DailyCachePolicy { Duration = new TimeSpan(1, 0, 0), RandomizerOffset = new TimeSpan(0, 30, 0) }; dcp.Reset(); Assert.IsTrue(dcp.Expiry.HasValue); Assert.IsTrue(dcp.Expiry.Value > new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0) && dcp.Expiry.Value < new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0).AddMinutes(30)); }
public void Flush() { CachePolicyManager.Reset(); using (var dsc = new DictionarySetCache <int, string>((data) => new KeyValuePair <int, string>[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2") })) { var policy = new DailyCachePolicy(); CachePolicyManager.Set(dsc.PolicyKey, policy); Assert.IsTrue(dsc.ContainsKey(1)); Assert.IsTrue(dsc.ContainsKey(2)); Assert.AreEqual(1, dsc.GetPolicy().Hits); dsc.Flush(true); Assert.IsTrue(dsc.ContainsKey(1)); Assert.IsTrue(dsc.ContainsKey(2)); Assert.AreEqual(1, dsc.GetPolicy().Hits); } }
public void PolicyManager() { Policy.CachePolicyManagerTest.TestSetUp(); var i = 0; var mtc = new KeyValueCache <int, string>((key) => { i++; return(key.ToString()); }, "KeyValueCacheTest"); Assert.IsNotNull(mtc.PolicyKey); var policy = new DailyCachePolicy(); CachePolicyManager.Current.Set(mtc.PolicyKey, policy); var policy2 = mtc.GetPolicy(); Assert.IsNotNull(policy2); Assert.AreSame(policy, policy2); var pa = CachePolicyManager.Current.GetPolicies(); Assert.AreEqual(2, pa.Length); // Check the internal nocachepolicy. var p0 = pa.Where(x => x.Key.StartsWith("KeyValueCacheTest_")).SingleOrDefault(); Assert.IsNotNull(p0); Assert.IsInstanceOf(typeof(NoCachePolicy), p0.Value); // Check the default policy for type. var p1 = pa.Where(x => x.Key == "KeyValueCacheTest").SingleOrDefault(); Assert.IsNotNull(p1); Assert.IsInstanceOf(typeof(DailyCachePolicy), p1.Value); // Get (add) new item to cache. var s = mtc[1]; Assert.AreEqual("1", s); Assert.AreEqual(1, i); // No new globally managed policies should have been created. pa = CachePolicyManager.Current.GetPolicies(); Assert.AreEqual(2, pa.Length); // Check policy for item is DailyCachePolicy but has its own instance. policy2 = mtc.GetPolicyByKey(1); Assert.IsNotNull(policy2); Assert.IsInstanceOf(typeof(DailyCachePolicy), policy2); Assert.AreNotSame(policy, policy2); // There should be no policy where item not found. Assert.IsNull(mtc.GetPolicyByKey(2)); // Flush cache where not expired. mtc.Flush(); s = mtc[1]; Assert.AreEqual("1", s); Assert.AreEqual(1, i); // Force flush; should reload cache after. CachePolicyManager.Current.ForceFlush(); s = mtc[1]; Assert.AreEqual("1", s); Assert.AreEqual(2, i); }