コード例 #1
0
        public async Task DoesNotCallFactoryWhenAlreadyCached()
        {
            var factoryCallCount = 0;

            Task <int> factory(string s) => Task.FromResult(factoryCallCount++);

            var cache = new AsyncAgedCache <string, int>(factory);

            await cache.GetOrAddAsync("abc", TimeSpan.FromSeconds(30));

            Assert.Equal(1, factoryCallCount);
            await cache.GetOrAddAsync("abc", TimeSpan.FromSeconds(30));

            Assert.Equal(1, factoryCallCount);
        }
コード例 #2
0
        public async Task DoesNotCallFactoryWithWrongKey()
        {
            var dictionary = new Dictionary <string, int>();

            dictionary["a"] = 0;
            dictionary["b"] = 0;

            Task <int> factoryA(string s) => Task.FromResult(dictionary[s]++);

            var cache = new AsyncAgedCache <string, int>(factoryA);

            await cache.GetOrAddAsync("a", TimeSpan.FromSeconds(30));

            Assert.Equal(1, dictionary["a"]);
            Assert.Equal(0, dictionary["b"]);
            await cache.GetOrAddAsync("b", TimeSpan.FromSeconds(30));

            Assert.Equal(1, dictionary["b"]);
        }