コード例 #1
0
        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();
            }
        }
コード例 #2
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");
        }