Beispiel #1
0
        public async Task PurgingShouldNotAffectResults()
        {
            var cache = new AsyncCache <int, int>(x => x * x)
            {
                ItemLimit = 10 // this should force a lot of recycling
            };
            var threadActive = true;
            var thread       = new Thread(() => { while (threadActive)
                                                  {
                                                      cache.CleanupAsync().Wait();
                                                  }
                                          });

            thread.Start();
            Func <int, Task> job = n => Task.Run(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    cache.GetValue(i);
                }
            });
            await Task.WhenAll(Enumerable.Range(1, 10).Select(job).ToList());

            threadActive = false;
            thread.Join();
        }
Beispiel #2
0
        public async Task AfterCachingValuesCacheShouldHaveHighMultithreadedPerformance()
        {
            var cache = new AsyncCache <int, int>(x => x * x)
            {
                ItemLimit        = 800,
                AutomaticCleanup = false
            };
            // below active count
            // dry run
            Func <int, Task> job = n => Task.Run(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    cache.GetValue(i % 1000);
                }
            });

            // performs optimizations
            cache.OptimizeEvery = TimeSpan.Zero; // after any time passed
            await cache.CleanupAsync();

            Stopwatch stopwatch = new Stopwatch();

            // benchmark
            stopwatch.Start();
            await Task.WhenAll(Enumerable.Range(1, 10).Select(job).ToList());

            stopwatch.Stop();
            var elapsed = stopwatch.Elapsed;

            elapsed.Should().BeLessThan(new TimeSpan(0, 0, 0, 1));
        }
Beispiel #3
0
        public void BackgroundThreadPerformingCleanupShouldNotAlterResults()
        {
            var cache        = new AsyncCache <int, int>(x => x * x);
            var threadActive = true;
            var thread       = new Thread(() => { while (threadActive)
                                                  {
                                                      cache.CleanupAsync().Wait();
                                                  }
                                          });

            thread.Start();
            for (int i = 0; i < 2000; i++)
            {
                cache.GetValue(i).Should().Be(i * i);
            }
            threadActive = false;
            thread.Join();
        }