示例#1
0
        public void Should_add_item_to_cache_when_context_exists()
        {
            SetupContext();

            sut.Add("Key", 1);

            var found = sut.TryGetValue("Key", out var value);

            Assert.True(found);
            Assert.Equal(1, value);

            sut.Remove("Key");

            var foundAfterRemove = sut.TryGetValue("Key", out value);

            Assert.False(foundAfterRemove);
            Assert.Null(value);
        }
示例#2
0
        public static T GetOrCreate <T>(this IRequestCache cache, object key, Func <T> task)
        {
            if (cache.TryGetValue(key, out var value) && value is T typedValue)
            {
                return(typedValue);
            }

            typedValue = task();

            cache.Add(key, typedValue);

            return(typedValue);
        }