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); }
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); }