예제 #1
0
파일: CacheCoreBase.cs 프로젝트: ostat/Beef
        /// <summary>
        /// Registers an internal <b>Policy Key</b> (using the <see cref="InternalPolicyKeyFormat"/>) with a <see cref="NoCachePolicy"/> to ensure always invoked;
        /// it is then expected that the <see cref="OnFlushCache(bool)"/> will handle policy expiration specifically.
        /// </summary>
        protected void RegisterInternalPolicyKey()
        {
            var overridePolicyKey = string.Format(System.Globalization.CultureInfo.InvariantCulture, InternalPolicyKeyFormat, PolicyKey);

            CachePolicyManager.Register(this, overridePolicyKey);
            CachePolicyManager.Set(overridePolicyKey, new NoCachePolicy());
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Registers an internal <b>Policy Key</b> (using the <see cref="InternalPolicyKeyFormat"/>) with a <see cref="NoCachePolicy"/> to ensure always invoked;
        /// it is then expected that the <see cref="OnFlushCache(bool)"/> will handle policy expiration specifically.
        /// </summary>
        protected void RegisterInternalPolicyKey()
        {
            var overridePolicyKey = string.Format(InternalPolicyKeyFormat, PolicyKey);

            CachePolicyManager.Register(this, overridePolicyKey);
            CachePolicyManager.Set(overridePolicyKey, new NoCachePolicy());
        }
예제 #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);
            }
        }
예제 #5
0
        public void PolicyManager()
        {
            CachePolicyManager.Reset();

            var i   = 0;
            var mtc = new KeyValueCache <int, string>((key) => { i++; return(key.ToString()); }, "KeyValueCacheTest");

            Assert.IsNotNull(mtc.PolicyKey);

            var policy = new DailyCachePolicy();

            CachePolicyManager.Set(mtc.PolicyKey, policy);

            var policy2 = mtc.GetPolicy();

            Assert.IsNotNull(policy2);
            Assert.AreSame(policy, policy2);

            var pa = CachePolicyManager.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.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.ForceFlush();
            s = mtc[1];
            Assert.AreEqual("1", s);
            Assert.AreEqual(2, i);
        }