示例#1
0
        public async Task CanFilterValues()
        {
            var counter = 0;
            var cache   = new AsyncCache <int, int>(x =>
            {
                counter++;
                return(x * x);
            });

            cache.Filter = x => x != 0;  // only cache non zero results
            cache.GetValue(1);
            cache.GetValue(1);
            counter.Should().Be(1);
            cache.GetValue(0);
            cache.GetValue(0);
            counter.Should().Be(3);
            await cache.GetValueAsync(1);

            await cache.GetValueAsync(1);

            counter.Should().Be(3);
            await cache.GetValueAsync(0);

            await cache.GetValueAsync(0);

            counter.Should().Be(5);
        }
示例#2
0
        public async Task CanOverrideFactoryWhenGettingValue()
        {
            var cache = new AsyncCache <int, int>(x => x * x);

            (await cache.GetValueAsync(3, x => x + x)).Should().Be(6);
            cache.GetValue(3, x => x + x).Should().Be(6);
        }
示例#3
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();
        }
示例#4
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));
        }
示例#5
0
        public void AsyncCache_Lazy_Created()
        {
            _cache.Clear();

            for (var i = 0; i < TestCount; i++)
            {
                Assert.AreEqual(Math.Pow(i, 2), _cache.GetValue(i).Result);
            }
        }
示例#6
0
        public async Task WillCacheFactoryResult(bool blockingGet, bool asyncFactory, bool useTasks)
        {
            int counter = 0;
            var cache   = new AsyncCache <int, int>();

            if (asyncFactory)
            {
                cache.AsyncFactory = async x =>
                {
                    await Task.Delay(1);

                    Interlocked.Increment(ref counter);
                    return(x * x);
                };
            }
            else
            {
                cache.Factory = x =>
                {
                    Interlocked.Increment(ref counter);
                    return(x * x);
                };
            }

            async Task Job()
            {
                for (int i = 0; i < 10; i++)
                {
                    int value;
                    if (blockingGet)
                    {
                        value = cache.GetValue(i);
                    }
                    else
                    {
                        value = await cache.GetValueAsync(i);
                    }
                    value.Should().Be(i * i);
                }
            }

            if (useTasks)
            {
                var tasks = Enumerable.Range(1, 10).Select(n => Task.Run((Func <Task>)Job));
                await Task.WhenAll(tasks);
            }
            else
            {
                // prepare the threads
                var threads = Enumerable.Range(1, 10).Select(n => new Thread(() => Job().Wait())).ToList();
                // start all the theads
                threads.ForEach(thread => thread.Start());
                threads.ForEach(thread => thread.Join());
            }
            // done? => counter should be 10
            counter.Should().Be(10);
        }
示例#7
0
        public async Task CanProvideFactoryAtCallTime()
        {
            var cache  = new AsyncCache <int, int>();
            var result = cache.GetValue(3, x => x * x);;

            result.Should().Be(9);
            var result2 = await cache.GetValueAsync(5, x => x + x);;

            result2.Should().Be(10);
        }
示例#8
0
        public async Task CacheMissActionCanDenyFactoryCall()
        {
            var cache   = new AsyncCache <int, int>(x => x * x);
            var options = new CacheCallOptions <int, int>();

            options.CacheMissAction = () =>
            {
                options.DontCallFactory = true;
            };
            cache.GetValue(3, options).Should().Be(0);
            (await cache.GetValueAsync(3, options)).Should().Be(0);
        }
示例#9
0
        public void Test_CoordinationDataStructures_AsyncCoordination_AsyncCache_Cache()
        {
            //异步cached
            var asyncCache = new AsyncCache <int, int>(e => Task.Factory.StartNew <int>(() => 10));
            var task       = asyncCache.GetValue(1);

            Assert.AreEqual(true, task.IsCompleted);
            Assert.AreEqual(1, task.Result);

            var httpCache = new HtmlAsyncCache();
            var pageTask  = httpCache.GetValue(new Uri("http://www.baidu.com"));

            Assert.AreEqual(true, pageTask.IsCompleted);
        }
示例#10
0
        public void CanRegisterDefaultOptions()
        {
            var count   = 0;
            var cache   = new AsyncCache <int, int>(x => x * x);
            var options = new CacheCallOptions <int, int>()
            {
                CacheMissAction = () =>
                {
                    count++;
                }
            };

            cache.DefaultCacheCallOptions = options;
            cache.GetValue(3);
            count.Should().Be(1);
        }
示例#11
0
        public async Task CanAddOptionsToEachRequest()
        {
            var count   = 0;
            var cache   = new AsyncCache <int, int>(x => x * x);
            var options = new CacheCallOptions <int, int>
            {
                CacheMissAction = () =>
                {
                    count++;
                }
            };

            cache.GetValue(3, options);            // call should miss
            await cache.GetValueAsync(3, options); // call should hit

            count.Should().Be(1);
        }
示例#12
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();
        }