コード例 #1
0
            public void Empty_cache_is_empty()
            {
                var cache = new InMemoryCache("fooCache");

                (cache as ISynchronousCache).Name.ShouldBe("fooCache");
                (cache as ISynchronousCache).TimeToLive.ShouldBeNull();
                (cache as ISynchronousCache).TimeToIdle.ShouldBeNull();
                cache.TotalSize.ShouldBe(0);
                cache.AccessCount.ShouldBe(0);
                cache.HitCount.ShouldBe(0);
                cache.MissCount.ShouldBe(0);
                cache.GetHitRatio().ShouldBe(0);
            }
コード例 #2
0
            public void Cache_access_miss()
            {
                var cache = new InMemoryCache("fooCache");
                var iface = cache as ISynchronousCache;

                iface.Put("foo", DummyItem);

                iface.Get("baz").ShouldBeNull();
                cache.AccessCount.ShouldBe(1);
                cache.HitCount.ShouldBe(0);
                cache.MissCount.ShouldBe(1);
                cache.GetHitRatio().ShouldBe(0.0);
            }
コード例 #3
0
            public void Cache_access_hit()
            {
                var cache = new InMemoryCache("fooCache");
                var iface = cache as ISynchronousCache;

                iface.Put("foo", DummyItem);
                cache.TotalSize.ShouldBe(1);

                iface.Get("foo").ShouldBe(DummyItem);
                cache.AccessCount.ShouldBe(1);
                cache.HitCount.ShouldBe(1);
                cache.MissCount.ShouldBe(0);
                cache.GetHitRatio().ShouldBe(1.0);
            }
コード例 #4
0
            public async Task Accessing_cache_from_multiple_threads()
            {
                var cache = new InMemoryCache("fooCache");
                var iface = cache as IAsynchronousCache;
                await iface.PutAsync("foo", DummyItem);

                var tasks = from i in Enumerable.Range(0, 10)
                            select Task.Run(async () =>
                            {
                                await iface.PutAsync(i.ToString(), new Dictionary<string, object>() { [$"loop{i}"] = i });
                                (await iface.GetAsync(i.ToString())).ShouldContainKeyAndValue($"loop{i}", i);
                                (await iface.GetAsync("foo")).ShouldBe(DummyItem);
                                (await iface.GetAsync("baz")).ShouldBeNull();
                            });
                await Task.WhenAll(tasks);

                cache.TotalSize.ShouldBe(11);
                cache.AccessCount.ShouldBe(30);
                cache.HitCount.ShouldBe(20);
                cache.MissCount.ShouldBe(10);
                cache.GetHitRatio().ShouldBe(0.67, tolerance: 0.01);
            }
コード例 #5
0
            public async Task Multiple_cache_reads_and_writes_from_single_thread()
            {
                var cache = new InMemoryCache("fooCache");
                var iface = cache as IAsynchronousCache;
                await iface.PutAsync("foo", DummyItem);

                for (var i = 0; i < 10; i++)
                {
                    await iface.PutAsync(i.ToString(), new Dictionary<string, object>() { [$"loop{i}"] = i });
                    (await iface.GetAsync(i.ToString())).ShouldContainKeyAndValue($"loop{i}", i);
                    (await iface.GetAsync("foo")).ShouldBe(DummyItem);
                    (await iface.GetAsync("baz")).ShouldBeNull();
                }

                cache.TotalSize.ShouldBe(11);
                cache.AccessCount.ShouldBe(30);
                cache.HitCount.ShouldBe(20);
                cache.MissCount.ShouldBe(10);
                cache.GetHitRatio().ShouldBe(0.67, tolerance: 0.01);
            }
コード例 #6
0
            public async Task Cache_access_miss()
            {
                var cache = new InMemoryCache("fooCache");
                var iface = cache as IAsynchronousCache;

                await iface.PutAsync("foo", DummyItem);

                (await iface.GetAsync("baz")).ShouldBeNull();
                cache.AccessCount.ShouldBe(1);
                cache.HitCount.ShouldBe(0);
                cache.MissCount.ShouldBe(1);
                cache.GetHitRatio().ShouldBe(0.0);
            }
コード例 #7
0
            public void Accessing_cache_from_multiple_threads()
            {
                var cache = new InMemoryCache("fooCache");
                var iface = cache as ISynchronousCache;
                iface.Put("foo", DummyItem);

                Parallel.For(0, 10, i =>
                {
                    iface.Put(i.ToString(), new Dictionary<string, object>() { [$"loop{i}"] = i });
                    iface.Get(i.ToString()).ShouldContainKeyAndValue($"loop{i}", i);
                    iface.Get("foo").ShouldBe(DummyItem);
                    iface.Get("baz").ShouldBeNull();
                });

                cache.TotalSize.ShouldBe(11);
                cache.AccessCount.ShouldBe(30);
                cache.HitCount.ShouldBe(20);
                cache.MissCount.ShouldBe(10);
                cache.GetHitRatio().ShouldBe(0.67, tolerance: 0.01);
            }