Exemple #1
0
        private void ConcurrenyRun()
        {
            int key1Count = 0;
            int key2Count = 0;

            CachePolicyManager.Reset();

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

            var tasks = new Task[8];

            tasks[0] = Task.Run(() => mtc.GetByKey1(1));
            tasks[1] = Task.Run(() => mtc.GetByKey2("1"));
            tasks[2] = Task.Run(() => mtc.GetByKey1(1));
            tasks[3] = Task.Run(() => mtc.GetByKey2("1"));
            tasks[4] = Task.Run(() => mtc.GetByKey1(1));
            tasks[5] = Task.Run(() => mtc.GetByKey2("1"));
            tasks[6] = Task.Run(() => mtc.GetByKey1(1));
            tasks[7] = Task.Run(() => mtc.GetByKey2("1"));

            Task.WaitAll(tasks);

            Assert.AreEqual(1, key1Count);
            Assert.AreEqual(1, key2Count);
        }
Exemple #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);
        }
Exemple #3
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));
        }
        public void Concurrency()
        {
            var l = new object();

            CachePolicyManager.Reset();
            int i   = 0;
            var mtc = new KeyValueCache <int, string>((key) => { lock (l) { i++; } TestContext.WriteLine($"GetValue {key} [{System.Threading.Thread.CurrentThread.ManagedThreadId}]"); System.Threading.Thread.Sleep(20); return(key.ToString()); });

            // Set an execution context.
            var tasks = new Task[13];

            tasks[0]  = Task.Run(() => Timer(0, 1, () => { Assert.AreEqual(mtc[1], "1"); }));
            tasks[1]  = Task.Run(() => Timer(1, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[2]  = Task.Run(() => Timer(2, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[3]  = Task.Run(() => Timer(3, 3, () => { Assert.AreEqual(mtc[3], "3"); }));
            tasks[4]  = Task.Run(() => Timer(4, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[5]  = Task.Run(() => Timer(5, 1, () => { Assert.AreEqual(mtc[1], "1"); }));
            tasks[6]  = Task.Run(() => Timer(6, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[7]  = Task.Run(() => Timer(7, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[8]  = Task.Run(() => Timer(8, 3, () => { Assert.AreEqual(mtc[3], "3"); }));
            tasks[9]  = Task.Run(() => Timer(9, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[10] = Task.Run(() => Timer(10, 1, () => { Assert.AreEqual(mtc[1], "1"); }));
            tasks[11] = Task.Run(() => Timer(11, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[12] = Task.Run(() => Timer(12, 3, () => { Assert.AreEqual(mtc[3], "3"); }));

            Task.WaitAll(tasks);

            Assert.AreEqual(3, i);

            TestContext.WriteLine("Round 2");

            tasks     = new Task[13];
            tasks[0]  = Task.Run(() => Timer(0, 1, () => { Assert.AreEqual(mtc[1], "1"); }));
            tasks[1]  = Task.Run(() => Timer(1, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[2]  = Task.Run(() => Timer(2, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[3]  = Task.Run(() => Timer(3, 3, () => { Assert.AreEqual(mtc[3], "3"); }));
            tasks[4]  = Task.Run(() => Timer(4, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[5]  = Task.Run(() => Timer(5, 1, () => { Assert.AreEqual(mtc[1], "1"); }));
            tasks[6]  = Task.Run(() => Timer(6, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[7]  = Task.Run(() => Timer(7, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[8]  = Task.Run(() => Timer(8, 3, () => { Assert.AreEqual(mtc[3], "3"); }));
            tasks[9]  = Task.Run(() => Timer(9, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[10] = Task.Run(() => Timer(10, 1, () => { Assert.AreEqual(mtc[1], "1"); }));
            tasks[11] = Task.Run(() => Timer(11, 2, () => { Assert.AreEqual(mtc[2], "2"); }));
            tasks[12] = Task.Run(() => Timer(12, 3, () => { Assert.AreEqual(mtc[3], "3"); }));

            Task.WaitAll(tasks);

            Assert.AreEqual(3, i);
        }
        public void UnregisterAndReuse()
        {
            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") }, "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]);
        }
Exemple #6
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);
            }
        }
        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);
        }
        public void Exercise()
        {
            CachePolicyManager.Reset();
            ExecutionContext.Reset(false);

            int i   = 0;
            var rdc = new ReferenceDataMultiTenantCache <TestRdCollection, TestRd>(() => { i++; return(GetData()); });

            // Set an execution context.
            ExecutionContext.SetCurrent(new ExecutionContext {
                TenantId = GetGuid(1)
            });

            // Nothing loaded.
            Assert.AreEqual(0, i);
            Assert.AreEqual(0, rdc.Count);
            Assert.IsTrue(rdc.IsExpired);

            // Now loaded.
            var c = rdc.GetCollection();

            Assert.AreEqual(1, i);
            Assert.IsNotNull(c);
            Assert.AreEqual(2, c.ActiveList.Count);
            Assert.AreEqual(1, rdc.Count);
            Assert.IsFalse(rdc.IsExpired);

            // Same cached version.
            c = rdc.GetCollection();
            Assert.AreEqual(1, i);
            Assert.IsNotNull(c);
            Assert.AreEqual(2, c.ActiveList.Count);
            Assert.AreEqual(1, rdc.Count);
            Assert.IsFalse(rdc.IsExpired);

            // Change the execution context.
            ExecutionContext.Reset(false);
            ExecutionContext.SetCurrent(new ExecutionContext {
                TenantId = GetGuid(2)
            });

            // Now load new tenant.
            c = rdc.GetCollection();
            Assert.AreEqual(2, i);
            Assert.IsNotNull(c);
            Assert.AreEqual(3, c.ActiveList.Count);
            Assert.AreEqual(2, rdc.Count);
            Assert.IsFalse(rdc.IsExpired);

            // Flush the cache.
            rdc.Flush(true);
            Assert.AreEqual(2, i);
            Assert.IsTrue(rdc.IsExpired);
            Assert.AreEqual(0, rdc.Count);

            // New collection cached.
            c = rdc.GetCollection();
            Assert.AreEqual(3, i);
            Assert.IsNotNull(c);
            Assert.AreEqual(3, c.ActiveList.Count);
            Assert.AreEqual(1, rdc.Count);
            Assert.IsFalse(rdc.IsExpired);
        }
        public void Concurrency()
        {
            CachePolicyManager.Reset();
            ExecutionContext.Reset(false);

            int i   = 0;
            var rdc = new ReferenceDataMultiTenantCache <TestRdCollection, TestRd>(() => { i++; return(GetData()); });

            // Set an execution context.
            var tasks = new Task[10];

            tasks[0] = Task.Run(() => Timer(0, GetGuid(08), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(08)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[1] = Task.Run(() => Timer(1, GetGuid(09), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(09)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[2] = Task.Run(() => Timer(2, GetGuid(1), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(1)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[3] = Task.Run(() => Timer(3, GetGuid(2), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(2)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[4] = Task.Run(() => Timer(4, GetGuid(08), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(08)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[5] = Task.Run(() => Timer(5, GetGuid(09), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(09)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[6] = Task.Run(() => Timer(6, GetGuid(1), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(1)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[7] = Task.Run(() => Timer(7, GetGuid(2), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(2)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[8] = Task.Run(() => Timer(8, GetGuid(08), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(08)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[9] = Task.Run(() => Timer(9, GetGuid(09), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(09)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));

            Task.WaitAll(tasks);

            TestContext.WriteLine("ROUND TWO");

            tasks    = new Task[10];
            tasks[0] = Task.Run(() => Timer(0, GetGuid(08), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(08)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[1] = Task.Run(() => Timer(1, GetGuid(09), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(09)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[2] = Task.Run(() => Timer(2, GetGuid(1), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(1)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[3] = Task.Run(() => Timer(3, GetGuid(2), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(2)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[4] = Task.Run(() => Timer(4, GetGuid(08), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(08)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[5] = Task.Run(() => Timer(5, GetGuid(09), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(09)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[6] = Task.Run(() => Timer(6, GetGuid(1), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(1)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[7] = Task.Run(() => Timer(7, GetGuid(2), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(2)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[8] = Task.Run(() => Timer(8, GetGuid(08), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(08)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));
            tasks[9] = Task.Run(() => Timer(9, GetGuid(09), () => { ExecutionContext.SetCurrent(new ExecutionContext {
                    TenantId = GetGuid(09)
                }); Assert.IsTrue(rdc.GetCollection().ActiveList.Count > 0); }));

            Task.WaitAll(tasks);
        }
        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);
        }