Ejemplo n.º 1
0
        private static Task[] RandomCacheTasks()
        {
            var name  = DateTime.UtcNow.ToShortDateString();
            var cache = new InMemoryCache(name, CacheDurationInSeconds);

            var tasks1 = Enumerable.Range(1, 10)
                         .Select(x => cache.AddOrGetAsync($"foo-{x}",
                                                          async() =>
            {
                await Task.Delay(RandomDelay());
                return(x);
            }));

            var tasks2 = Enumerable.Range(1, 10)
                         .Select(x => cache.AddOrGetAsync($"bar-{x}",
                                                          async() =>
            {
                await Task.Delay(RandomDelay());
                return(x);
            }));

            var tasks3 = Enumerable.Range(1, 10)
                         .Select(_ => Task.Run(async() => await cache.ClearCacheEntriesAsync()));

            var tasks4 = Enumerable.Range(1, 10)
                         .Select(_ => Task.Delay(RandomDelay()));

            var array = tasks1.Union(tasks2).Union(tasks3).Union(tasks4);

            return(array.OrderBy(_ => Random.Next()).ToArray());
        }
Ejemplo n.º 2
0
        public async Task Get_New_Value_When_Delay_Is_Not_Expired_And_Cache_Was_Flushed()
        {
            var name  = DateTime.UtcNow.ToShortDateString();
            var cache = new InMemoryCache(name, CacheDurationInSeconds);

            cache.Size.Should().Be(0);

            var xx = await cache.AddOrGetAsync("foo",
                                               async() =>
            {
                await Task.Delay(RandomDelay());
                return(10);
            }).ConfigureAwait(false);

            cache.Size.Should().Be(1);
            xx.Should().Be(10);

            await Task.Delay(RandomDelay());

            await cache.ClearCacheEntriesAsync();

            cache.Size.Should().Be(0);

            var yy = await cache.AddOrGetAsync("foo",
                                               async() =>
            {
                await Task.Delay(RandomDelay());
                return(20);
            }).ConfigureAwait(false);

            cache.Size.Should().Be(1);
            yy.Should().Be(20);
        }
Ejemplo n.º 3
0
        public async Task Should_Not_Throw_Exception_When_Cache_Is_Flushed_While_Is_Used_V2()
        {
            var name  = DateTime.UtcNow.ToShortDateString();
            var cache = new InMemoryCache(name, CacheDurationInSeconds);

            async Task FuncBody(int index)
            {
                var key = Guid.NewGuid().ToString();

                var xx = await cache.AddOrGetAsync(key, async() =>
                {
                    await Task.Delay(RandomDelay());
                    return(10);
                });

                xx.Should().Be(10);

                await Task.Run(async() =>
                {
                    await Task.Delay(RandomDelay());
                    await cache.ClearCacheEntriesAsync();
                });

                var yy = await cache.AddOrGetAsync(key, async() =>
                {
                    await Task.Delay(RandomDelay());
                    return(20);
                });

                yy.Should().Be(20);
            }

            var source = Enumerable.Range(1, 30);

            await ParallelForEachAsync(source, FuncBody, 5);

            cache.Size.Should().BeGreaterThan(0);
        }