Beispiel #1
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();
            }
        }
Beispiel #2
0
        public async Task GetOrSet_CacheHitBackgroundRefresh()
        {
            var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, Array.Empty <ICacheExtension>());
            var cacheEntry = new CacheEntry <int>(17, DateTime.UtcNow.AddDays(1));
            await cacheStack.SetAsync("GetOrSet_CacheHitBackgroundRefresh", cacheEntry);

            var waitingOnBackgroundTask = new TaskCompletionSource <int>();

            var result = await cacheStack.GetOrSetAsync <int>("GetOrSet_CacheHitBackgroundRefresh", (oldValue) =>
            {
                waitingOnBackgroundTask.TrySetResult(27);
                return(Task.FromResult(27));
            }, new CacheSettings(TimeSpan.FromDays(2), TimeSpan.Zero));

            Assert.AreEqual(17, result);

            await waitingOnBackgroundTask.Task;
            //Give 400ms to return the value and set it to the MemoryCacheLayer
            await Task.Delay(400);

            var refetchedResult = await cacheStack.GetAsync <int>("GetOrSet_CacheHitBackgroundRefresh");

            Assert.AreEqual(27, refetchedResult.Value);

            await DisposeOf(cacheStack);
        }
        public async Task GetOrSet_StaleCacheHit()
        {
            using var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, Array.Empty <ICacheExtension>());
            var cacheEntry = new CacheEntry <int>(17, DateTime.UtcNow.AddDays(2));
            await cacheStack.SetAsync("GetOrSet_StaleCacheHit", cacheEntry);

            Internal.DateTimeProvider.UpdateTime();

            var refreshWaitSource = new TaskCompletionSource <bool>();

            var result = await cacheStack.GetOrSetAsync <int>("GetOrSet_StaleCacheHit", (oldValue) =>
            {
                Assert.AreEqual(17, oldValue);
                refreshWaitSource.TrySetResult(true);
                return(Task.FromResult(27));
            }, new CacheSettings(TimeSpan.FromDays(2), TimeSpan.Zero));

            Assert.AreEqual(17, result);

            await Task.WhenAny(refreshWaitSource.Task, Task.Delay(TimeSpan.FromSeconds(5)));

            var refetchedResult = await cacheStack.GetAsync <int>("GetOrSet_StaleCacheHit");

            Assert.AreEqual(27, refetchedResult.Value);
        }
Beispiel #4
0
        public async Task Get_ThrowsOnUseAfterDisposal()
        {
            var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, null);

            await DisposeOf(cacheStack);

            await cacheStack.GetAsync <int>("KeyDoesntMatter");
        }
Beispiel #5
0
 public async Task GetMiss()
 {
     await using (var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, Array.Empty <ICacheExtension>()))
     {
         for (var i = 0; i < WorkIterations; i++)
         {
             await cacheStack.GetAsync <int>("GetMiss");
         }
     }
 }
Beispiel #6
0
        public async Task GetHit()
        {
            await using (var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, Array.Empty <ICacheExtension>()))
            {
                await cacheStack.SetAsync("GetHit", 15, TimeSpan.FromDays(1));

                for (var i = 0; i < WorkIterations; i++)
                {
                    await cacheStack.GetAsync <int>("GetHit");
                }
            }
        }
Beispiel #7
0
 public async Task CacheTower_RedisCacheLayer()
 {
     await using (var cacheStack = new CacheStack(new[] { new RedisCacheLayer(RedisHelper.GetConnection()) }, Array.Empty <ICacheExtension>()))
     {
         await LoopActionAsync(Iterations, async() =>
         {
             await cacheStack.SetAsync("TestKey", 123, TimeSpan.FromDays(1));
             await cacheStack.GetAsync <int>("TestKey");
             await cacheStack.GetOrSetAsync <string>("GetOrSet_TestKey", (old) =>
             {
                 return(Task.FromResult("Hello World"));
             }, new CacheSettings(TimeSpan.FromDays(1), TimeSpan.FromDays(1)));
         });
     }
 }
Beispiel #8
0
 public async Task Get_ThrowsOnNullKey()
 {
     var cacheStack = new CacheStack(new[] { new MemoryCacheLayer() }, Array.Empty <ICacheExtension>());
     await cacheStack.GetAsync <int>(null);
 }