コード例 #1
0
        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);
        }
コード例 #2
0
        public void GetAndContainsKey()
        {
            var dsc = new DictionarySetCache <int, string>((data) => new KeyValuePair <int, string>[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2") });

            Assert.IsTrue(dsc.ContainsKey(1));
            Assert.IsTrue(dsc.ContainsKey(2));
            Assert.IsFalse(dsc.ContainsKey(3));

            Assert.IsTrue(dsc.TryGetByKey(1, out string val));
            Assert.AreEqual("1", val);

            Assert.IsTrue(dsc.TryGetByKey(2, out val));
            Assert.AreEqual("2", val);

            Assert.IsFalse(dsc.TryGetByKey(3, out val));
            Assert.IsNull(val);
        }
コード例 #3
0
ファイル: CachePolicyManagerTest.cs プロジェクト: ostat/Beef
        public void UnregisterAndReuse()
        {
            TestSetUp();
            var dsc = new DictionarySetCache <int, string>((data) => new KeyValuePair <int, string>[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2") }, "CachePolicyManagerTest");

            // Asserting will load the cache on first access.
            Assert.AreEqual("1", dsc[1]);
            Assert.AreEqual("2", dsc[2]);

            dsc.Dispose();

            // Unregister so the policy name can be reused.
            //CachePolicyManager.Unregister(dsc.PolicyKey);

            dsc = new DictionarySetCache <int, string>((data) => new KeyValuePair <int, string>[] { new KeyValuePair <int, string>(1, "10"), new KeyValuePair <int, string>(2, "20") }, "CachePolicyManagerTest");
            Assert.AreEqual("10", dsc[1]);
            Assert.AreEqual("20", dsc[2]);
        }
コード例 #4
0
        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);
            }
        }