示例#1
0
        protected static void AssertComplexTypeCaching(ISyncCacheLayer cacheLayer)
        {
            var complexTypeOneEntry = new CacheEntry <ComplexTypeCaching_TypeOne>(new ComplexTypeCaching_TypeOne
            {
                ExampleString = "Hello World",
                ExampleNumber = 99,
                ListOfNumbers = new List <int>()
                {
                    1, 2, 4, 8
                }
            }, TimeSpan.FromDays(1));

            cacheLayer.Set("ComplexTypeOne", complexTypeOneEntry);
            var complexTypeOneEntryGet = cacheLayer.Get <ComplexTypeCaching_TypeOne>("ComplexTypeOne");

            Assert.AreEqual(complexTypeOneEntry, complexTypeOneEntryGet, "Set value in cache doesn't match retrieved value");

            var complexTypeTwoEntry = new CacheEntry <ComplexTypeCaching_TypeTwo>(new ComplexTypeCaching_TypeTwo
            {
                ExampleString       = "Hello World",
                ArrayOfObjects      = new[] { complexTypeOneEntry.Value },
                DictionaryOfNumbers = new Dictionary <string, int>()
                {
                    { "A", 1 }, { "Z", 26 }
                }
            }, TimeSpan.FromDays(1));

            cacheLayer.Set("ComplexTypeTwo", complexTypeTwoEntry);
            var complexTypeTwoEntryGet = cacheLayer.Get <ComplexTypeCaching_TypeTwo>("ComplexTypeTwo");

            Assert.AreEqual(complexTypeTwoEntry, complexTypeTwoEntryGet, "Set value in cache doesn't match retrieved value");
        }
        protected void BenchmarkWork(ISyncCacheLayer cacheLayer)
        {
            for (var iterationCount = 0; iterationCount < WorkIterations; iterationCount++)
            {
                //Get 100 misses
                for (var i = 0; i < 100; i++)
                {
                    cacheLayer.Get <int>("GetMiss_" + i);
                }

                var startDate = DateTime.UtcNow.AddDays(-50);

                //Set first 100 (simple type)
                for (var i = 0; i < 100; i++)
                {
                    cacheLayer.Set("Comparison_" + i, new CacheEntry <int>(1, startDate.AddDays(i) + TimeSpan.FromDays(1)));
                }
                //Set last 100 (complex type)
                for (var i = 100; i < 200; i++)
                {
                    cacheLayer.Set("Comparison_" + i, new CacheEntry <ComplexType>(new ComplexType
                    {
                        ExampleString       = "Hello World",
                        ExampleNumber       = 42,
                        ExampleDate         = new DateTime(2000, 1, 1),
                        DictionaryOfNumbers = new Dictionary <string, int>()
                        {
                            { "A", 1 }, { "B", 2 }, { "C", 3 }
                        }
                    }, startDate.AddDays(i - 100) + TimeSpan.FromDays(1)));
                }

                //Get first 50 (simple type)
                for (var i = 0; i < 50; i++)
                {
                    cacheLayer.Get <int>("Comparison_" + i);
                }
                //Get last 50 (complex type)
                for (var i = 150; i < 200; i++)
                {
                    cacheLayer.Get <ComplexType>("Comparison_" + i);
                }

                //Evict middle 100
                for (var i = 50; i < 150; i++)
                {
                    cacheLayer.Evict("Comparison_" + i);
                }

                //Cleanup outer 100
                cacheLayer.Cleanup();
            }
        }
示例#3
0
        protected static void AssertGetSetCache(ISyncCacheLayer cacheLayer)
        {
            var cacheEntry = new CacheEntry <int>(12, TimeSpan.FromDays(1));

            cacheLayer.Set("AssertGetSetCache", cacheEntry);
            var cacheEntryGet = cacheLayer.Get <int>("AssertGetSetCache");

            Assert.AreEqual(cacheEntry, cacheEntryGet, "Set value in cache doesn't match retrieved value");
        }
示例#4
0
        protected static void AssertCacheEviction(ISyncCacheLayer cacheLayer)
        {
            var cacheEntry = new CacheEntry <int>(77, TimeSpan.FromDays(1));

            cacheLayer.Set("AssertCacheEviction-ToEvict", cacheEntry);
            cacheLayer.Set("AssertCacheEviction-ToKeep", cacheEntry);

            var cacheEntryGetPreEviction1 = cacheLayer.Get <int>("AssertCacheEviction-ToEvict");
            var cacheEntryGetPreEviction2 = cacheLayer.Get <int>("AssertCacheEviction-ToKeep");

            Assert.IsNotNull(cacheEntryGetPreEviction1, "Value not set in cache");
            Assert.IsNotNull(cacheEntryGetPreEviction2, "Value not set in cache");

            cacheLayer.Evict("AssertCacheEviction-ToEvict");

            var cacheEntryGetPostEviction1 = cacheLayer.Get <int>("AssertCacheEviction-ToEvict");
            var cacheEntryGetPostEviction2 = cacheLayer.Get <int>("AssertCacheEviction-ToKeep");

            Assert.IsNull(cacheEntryGetPostEviction1, "Didn't evict value that should have been");
            Assert.IsNotNull(cacheEntryGetPostEviction2, "Evicted entry that should have been kept");
        }
示例#5
0
        protected static void AssertCacheCleanup(ISyncCacheLayer cacheLayer)
        {
            CacheEntry <int> DoCleanupTest(DateTime dateTime)
            {
                var cacheKey = $"AssertCacheCleanup-(DateTime:{dateTime})";

                var cacheEntry = new CacheEntry <int>(98, dateTime);

                cacheLayer.Set(cacheKey, cacheEntry);

                cacheLayer.Cleanup();

                return(cacheLayer.Get <int>(cacheKey));
            }

            Assert.IsNotNull(DoCleanupTest(DateTime.UtcNow.AddDays(1)), "Cleanup removed entry that was still live");
            Assert.IsNull(DoCleanupTest(DateTime.UtcNow.AddDays(-1)), "Cleanup kept entry past the end of life");
        }