public async ValueTask Benchmark()
        {
            var gettingLockSource     = new TaskCompletionSource <bool>();
            var continueRefreshSource = new TaskCompletionSource <bool>();

            _ = CacheStack.GetOrSetAsync <int>("RefreshWaiting", async(old) =>
            {
                gettingLockSource.SetResult(true);
                await continueRefreshSource.Task;
                return(42);
            }, new CacheSettings(TimeSpan.FromDays(1), TimeSpan.FromDays(1)));

            await gettingLockSource.Task;

            var awaitingTasks = new List <Task>();

            for (var i = 0; i < WorkIterations; i++)
            {
                var task = CacheStack.GetOrSetAsync <int>("RefreshWaiting", (old) =>
                {
                    return(Task.FromResult(99));
                }, new CacheSettings(TimeSpan.FromDays(1), TimeSpan.FromDays(1)));
                awaitingTasks.Add(task.AsTask());
            }

            continueRefreshSource.SetResult(true);

            await Task.WhenAll(awaitingTasks);

            //Remove value for next run
            await CacheStack.EvictAsync("RefreshWaiting");
        }
예제 #2
0
        public async Task Evict_ThrowsOnUseAfterDisposal()
        {
            var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, null);

            await DisposeOf(cacheStack);

            await cacheStack.EvictAsync("KeyDoesntMatter");
        }
        public async Task Evict_TriggersCacheChangeExtension()
        {
            var mockExtension = new Mock <ICacheChangeExtension>();

            using var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, new [] { mockExtension.Object });
            var cacheEntry = await cacheStack.SetAsync("Evict_TriggerCacheChangeExtension", 42, TimeSpan.FromDays(1));

            await cacheStack.EvictAsync("Evict_TriggerCacheChangeExtension");

            mockExtension.Verify(e => e.OnCacheEvictionAsync("Evict_TriggerCacheChangeExtension"), Times.Once);
        }
예제 #4
0
        public async Task Evict_TwoLayers()
        {
            await using (var cacheStack = new CacheStack(new[] { new MemoryCacheLayer(), new MemoryCacheLayer() }, Array.Empty <ICacheExtension>()))
            {
                for (var i = 0; i < WorkIterations; i++)
                {
                    await cacheStack.SetAsync("Evict", 15, TimeSpan.FromDays(1));

                    await cacheStack.EvictAsync("Evict");
                }
            }
        }
예제 #5
0
        public async Task MemoryCacheLayer_CacheStack()
        {
            await using (var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, null))
            {
                //Get 100 misses
                for (var i = 0; i < 100; i++)
                {
                    await cacheStack.GetAsync <int>("GetMiss_" + i);
                }

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

                //Set first 100 (simple type)
                for (var i = 0; i < 100; i++)
                {
                    await cacheStack.SetAsync("Comparison_" + i, new CacheEntry <int>(1, startDate.AddDays(i) + TimeSpan.FromDays(1)));
                }
                //Set last 100 (complex type)
                for (var i = 100; i < 200; i++)
                {
                    await cacheStack.SetAsync("Comparison_" + i, new CacheEntry <RealCostComplexType>(new RealCostComplexType
                    {
                        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++)
                {
                    await cacheStack.GetAsync <int>("Comparison_" + i);
                }
                //Get last 50 (complex type)
                for (var i = 150; i < 200; i++)
                {
                    await cacheStack.GetAsync <RealCostComplexType>("Comparison_" + i);
                }

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

                //Cleanup outer 100
                await cacheStack.CleanupAsync();
            }
        }
        public async Task Evict_EvictsAllTheLayers()
        {
            var layer1 = new MemoryCacheLayer();
            var layer2 = new MemoryCacheLayer();

            using var cacheStack = new CacheStack(new[] { layer1, layer2 }, Array.Empty <ICacheExtension>());
            var cacheEntry = await cacheStack.SetAsync("Evict_EvictsAllTheLayers", 42, TimeSpan.FromDays(1));

            Assert.AreEqual(cacheEntry, await layer1.GetAsync <int>("Evict_EvictsAllTheLayers"));
            Assert.AreEqual(cacheEntry, await layer2.GetAsync <int>("Evict_EvictsAllTheLayers"));

            await cacheStack.EvictAsync("Evict_EvictsAllTheLayers");

            Assert.IsNull(await layer1.GetAsync <int>("Evict_EvictsAllTheLayers"));
            Assert.IsNull(await layer2.GetAsync <int>("Evict_EvictsAllTheLayers"));
        }
예제 #7
0
 public async Task Evict_ThrowsOnNullKey()
 {
     var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, Array.Empty <ICacheExtension>());
     await cacheStack.EvictAsync(null);
 }