diff --git a/LazyCache.UnitTestsCore21/CachingServiceTests.cs b/LazyCache.UnitTestsCore21/CachingServiceTests.cs index 6961def..3867362 100644 --- a/LazyCache.UnitTestsCore21/CachingServiceTests.cs +++ b/LazyCache.UnitTestsCore21/CachingServiceTests.cs @@ -1,6 +1,8 @@ using LazyCache.Providers; using Microsoft.Extensions.Caching.Memory; using NUnit.Framework; +using System.Threading.Tasks; +using System; namespace LazyCache.UnitTestsCore21 { @@ -32,5 +34,53 @@ public void GetOrAddOnCore21ReturnsTheCachedItem() Assert.IsNotNull(cachedResult); Assert.AreEqual("SomeValue", cachedResult.SomeProperty); } + + [Test] + public async Task GetOrAddAsyncOnCore21DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } + + [Test] + public async Task GetOrAddOnCore21DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } } } \ No newline at end of file diff --git a/LazyCache.UnitTestsCore22/CachingServiceTests.cs b/LazyCache.UnitTestsCore22/CachingServiceTests.cs index 77c90a7..9ab278d 100644 --- a/LazyCache.UnitTestsCore22/CachingServiceTests.cs +++ b/LazyCache.UnitTestsCore22/CachingServiceTests.cs @@ -1,6 +1,8 @@ using LazyCache.Providers; using Microsoft.Extensions.Caching.Memory; using NUnit.Framework; +using System.Threading.Tasks; +using System; namespace LazyCache.UnitTestsCore22 { @@ -32,5 +34,53 @@ public void GetOrAddOnCore22ReturnsTheCachedItem() Assert.IsNotNull(cachedResult); Assert.AreEqual("SomeValue", cachedResult.SomeProperty); } + + [Test] + public async Task GetOrAddAsyncOnCore22DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } + + [Test] + public async Task GetOrAddOnCore22DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } } } \ No newline at end of file diff --git a/LazyCache.UnitTestsCore30/CachingServiceTests.cs b/LazyCache.UnitTestsCore30/CachingServiceTests.cs index 9934475..499b453 100644 --- a/LazyCache.UnitTestsCore30/CachingServiceTests.cs +++ b/LazyCache.UnitTestsCore30/CachingServiceTests.cs @@ -1,6 +1,8 @@ using LazyCache.Providers; using Microsoft.Extensions.Caching.Memory; using NUnit.Framework; +using System.Threading.Tasks; +using System; namespace LazyCache.UnitTestsCore30 { @@ -32,5 +34,53 @@ public void GetOrAddOnCore30ReturnsTheCachedItem() Assert.IsNotNull(cachedResult); Assert.AreEqual("SomeValue", cachedResult.SomeProperty); } + + [Test] + public async Task GetOrAddAsyncOnCore30DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } + + [Test] + public async Task GetOrAddOnCore30DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } } } \ No newline at end of file diff --git a/LazyCache.UnitTestsCore31/CachingServiceTests.cs b/LazyCache.UnitTestsCore31/CachingServiceTests.cs index 6e0d6d9..b7eee85 100644 --- a/LazyCache.UnitTestsCore31/CachingServiceTests.cs +++ b/LazyCache.UnitTestsCore31/CachingServiceTests.cs @@ -1,6 +1,8 @@ using LazyCache.Providers; using Microsoft.Extensions.Caching.Memory; using NUnit.Framework; +using System.Threading.Tasks; +using System; namespace LazyCache.UnitTestsCore31 { @@ -32,5 +34,53 @@ public void GetOrAddOnCore31ReturnsTheCachedItem() Assert.IsNotNull(cachedResult); Assert.AreEqual("SomeValue", cachedResult.SomeProperty); } + + [Test] + public async Task GetOrAddAsyncOnCore31DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } + + [Test] + public async Task GetOrAddOnCore31DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } } } \ No newline at end of file diff --git a/LazyCache.UnitTestsNet50/CachingServiceTests.cs b/LazyCache.UnitTestsNet50/CachingServiceTests.cs index bb7a979..6b81940 100644 --- a/LazyCache.UnitTestsNet50/CachingServiceTests.cs +++ b/LazyCache.UnitTestsNet50/CachingServiceTests.cs @@ -1,6 +1,8 @@ using LazyCache.Providers; using Microsoft.Extensions.Caching.Memory; using NUnit.Framework; +using System.Threading.Tasks; +using System; namespace LazyCache.UnitTestsNet50 { @@ -32,5 +34,53 @@ public void GetOrAddOnNet50ReturnsTheCachedItem() Assert.IsNotNull(cachedResult); Assert.AreEqual("SomeValue", cachedResult.SomeProperty); } + + [Test] + public async Task GetOrAddAsyncOnNet50DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAddAsync("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } + + [Test] + public async Task GetOrAddOnNet50DefaultCacheDurationHonoured() + { + sut.DefaultCachePolicy.DefaultCacheDurationSeconds = 1; + + int value = DateTime.UtcNow.Second; + int result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + + Assert.AreEqual(value, result); + + // wait for the item to expire + await Task.Delay(TimeSpan.FromSeconds(2)); + + // same key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("foo", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + + // new key + value = DateTime.UtcNow.Second; + result = await sut.GetOrAdd("bar", x => Task.FromResult(value)); + Assert.AreEqual(value, result); + } } } diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index df091de..3c2ab06 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -97,7 +97,7 @@ public virtual bool TryGetValue(string key, out T value) public virtual T GetOrAdd(string key, Func addItemFactory) { - return GetOrAdd(key, addItemFactory, null); + return GetOrAdd(key, addItemFactory, DefaultCachePolicy.BuildOptions()); } public virtual T GetOrAdd(string key, Func addItemFactory, MemoryCacheEntryOptions policy) @@ -180,7 +180,7 @@ public virtual void Remove(string key) public virtual Task GetOrAddAsync(string key, Func> addItemFactory) { - return GetOrAddAsync(key, addItemFactory, null); + return GetOrAddAsync(key, addItemFactory, DefaultCachePolicy.BuildOptions()); } public virtual async Task GetOrAddAsync(string key, Func> addItemFactory,