Beispiel #1
0
        // TEMPORARILY DISABLED BECAUSE DURING EXECUTION IT HANGS INDEFINITELY (MAYBE A DEADLOCK?)
        //[Benchmark]
        public async Task CacheTower()
        {
            await using (var cache = new CacheStack(new[] { new MemoryCacheLayer() }, new[] { new AutoCleanupExtension(TimeSpan.FromMinutes(5)) }))
            {
                var cacheSettings = new CacheSettings(CacheDuration);

                for (int i = 0; i < Rounds; i++)
                {
                    var tasks = new ConcurrentBag <Task>();

                    Parallel.ForEach(Keys, key =>
                    {
                        Parallel.For(0, Accessors, _ =>
                        {
                            var t = cache.GetOrSetAsync <SamplePayload>(
                                key,
                                async(old) =>
                            {
                                await Task.Delay(FactoryDurationMs).ConfigureAwait(false);
                                return(new SamplePayload());
                            },
                                cacheSettings
                                ).AsTask();
                            tasks.Add(t);
                        });
                    });

                    await Task.WhenAll(tasks).ConfigureAwait(false);
                }

                // CLEANUP
                await cache.FlushAsync();
            }
        }
        public async Task Flush_ThrowsOnUseAfterDisposal()
        {
            var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, null);

            using (cacheStack)
            { }

            await cacheStack.FlushAsync();
        }
        public async Task Flush_TriggersCacheChangeExtension()
        {
            var mockExtension = new Mock <ICacheChangeExtension>();

            using var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, new[] { mockExtension.Object });

            await cacheStack.FlushAsync();

            mockExtension.Verify(e => e.OnCacheFlushAsync(), Times.Once);
        }
        public async Task Flush_FlushesAllTheLayers()
        {
            var layer1 = new MemoryCacheLayer();
            var layer2 = new MemoryCacheLayer();

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

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

            await cacheStack.FlushAsync();

            Assert.IsNull(await layer1.GetAsync <int>("Flush_FlushesAllTheLayers"));
            Assert.IsNull(await layer2.GetAsync <int>("Flush_FlushesAllTheLayers"));
        }