Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void PolicyManager()
        {
            CachePolicyManager.Reset();

            var mtc = new TwoKeyValueCache <int, string, string>(
                (key1) => { return((key1 == 99) ? (false, null, null) : (true, key1.ToString(), $"x{key1}x")); },
                (key2) => { return(true, int.Parse(key2), $"x{int.Parse(key2)}x"); },
                "TwoKeyValueCacheTest");

            Assert.IsTrue(mtc.TryGetByKey1(1, out string val));
            Assert.IsTrue(mtc.TryGetByKey2("2", out val));

            var pa = CachePolicyManager.GetPolicies();

            Assert.AreEqual(2, pa.Length);

            // Check the internal nocachepolicy.
            var p0 = pa.Where(x => x.Key.StartsWith("TwoKeyValueCacheTest_")).SingleOrDefault();

            Assert.IsNotNull(p0);
            Assert.IsInstanceOf(typeof(NoCachePolicy), p0.Value);

            // Check the default policy for type.
            var p1 = pa.Where(x => x.Key == "TwoKeyValueCacheTest").SingleOrDefault();

            Assert.IsNotNull(p1);
            Assert.IsInstanceOf(typeof(NoExpiryCachePolicy), p1.Value);

            // Each value should have its own policy.
            var policy1 = mtc.GetPolicyByKey1(1);

            Assert.IsNotNull(policy1);
            Assert.IsInstanceOf(typeof(NoExpiryCachePolicy), policy1);

            var policy2 = mtc.GetPolicyByKey2("2");

            Assert.IsNotNull(policy2);
            Assert.IsInstanceOf(typeof(NoExpiryCachePolicy), policy2);
            Assert.AreNotSame(policy1, policy2);

            // There should be no policy where item not found.
            Assert.IsNull(mtc.GetPolicyByKey1(3));

            // Flush cache where not expired; nothing happens.
            mtc.Flush();
            var v = mtc.GetByKey1(1);

            Assert.AreEqual("x1x", v);
            Assert.AreEqual(2, mtc.Count);

            // Force flush; should reload cache after.
            mtc.Flush(true);
            Assert.AreEqual(0, mtc.Count);
            Assert.IsFalse(mtc.ContainsKey1(1));
        }
Ejemplo n.º 3
0
        public void CreateAndGetWithForceFlush()
        {
            CachePolicyManager.IsInternalTracingEnabled = true;
            CachePolicyManager.Reset();

            // Initialize the cache.
            var mtc = new MultiTenantCache <KeyValueCache <int, string> >((g, p) =>
            {
                if (g.Equals(ToGuid(1)))
                {
                    return(new KeyValueCache <int, string>(p, (k) => k.ToString()));
                }
                else
                {
                    return(new KeyValueCache <int, string>(p, (k) => "X" + k.ToString()));
                }
            }, "MultiTenantCacheTest");

            var pa = CachePolicyManager.GetPolicies();

            Assert.AreEqual(1, pa.Length);

            // Check the internal nocachepolicy.
            var p0 = pa.Where(x => x.Key.StartsWith("MultiTenantCacheTest_")).SingleOrDefault();

            Assert.IsNotNull(p0);
            Assert.IsInstanceOf(typeof(NoCachePolicy), p0.Value);

            // Check that the cache is empty.
            Assert.IsFalse(mtc.Contains(ToGuid(1)));
            Assert.AreEqual(0, mtc.Count);
            mtc.Remove(ToGuid(1));

            // Check the first tenant.
            var kvc1 = mtc.GetCache(ToGuid(1));

            Assert.IsNotNull(kvc1);
            Assert.AreEqual("1", kvc1[1]);
            Assert.AreEqual(1, mtc.Count);

            // Check the default policy for type.
            pa = CachePolicyManager.GetPolicies();
            Assert.AreEqual(2, pa.Length);

            var p1 = pa.Where(x => x.Key == "MultiTenantCacheTest").SingleOrDefault();

            Assert.IsNotNull(p1);
            Assert.IsInstanceOf(typeof(NoExpiryCachePolicy), p1.Value);

            // Check the second tenant.
            var kvc2 = mtc.GetCache(ToGuid(2));

            Assert.IsNotNull(kvc2);
            Assert.AreEqual("X1", kvc2[1]);
            Assert.AreEqual(2, mtc.Count);

            // No new PolicyManager policies should be created.
            pa = CachePolicyManager.GetPolicies();
            Assert.AreEqual(2, pa.Length);

            // Flush the cache - nothing should happen as they never expire.
            CachePolicyManager.Flush();
            Assert.AreEqual(2, mtc.Count);

            // Remove a tenant.
            mtc.Remove(ToGuid(2));
            Assert.IsTrue(mtc.Contains(ToGuid(1)));
            Assert.IsFalse(mtc.Contains(ToGuid(2)));
            Assert.AreEqual(1, mtc.Count);
            Assert.AreEqual(0, kvc2.Count);

            // Force flush the cache - should be removed.
            CachePolicyManager.ForceFlush();
            Assert.IsFalse(mtc.Contains(ToGuid(1)));
            Assert.IsFalse(mtc.Contains(ToGuid(2)));
            Assert.AreEqual(0, mtc.Count);
            Assert.AreEqual(0, kvc1.Count);
        }
Ejemplo n.º 4
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);
        }