From 1698c9a08fa514074de264bb72721a16a804dcaf Mon Sep 17 00:00:00 2001 From: touchofevil-dev <67504725+touchofevil-dev@users.noreply.github.com> Date: Fri, 12 May 2023 21:41:36 +0530 Subject: [PATCH 1/3] Fixed default cache duration not getting applied to instance methods Added unit tests --- .../CachingServiceTests.cs | 50 +++++++++++++++++++ .../LazyCache.UnitTestsCore21.csproj | 2 +- .../CachingServiceTests.cs | 50 +++++++++++++++++++ .../LazyCache.UnitTestsCore22.csproj | 2 +- .../CachingServiceTests.cs | 50 +++++++++++++++++++ .../LazyCache.UnitTestsCore30.csproj | 2 +- .../CachingServiceTests.cs | 50 +++++++++++++++++++ .../LazyCache.UnitTestsCore31.csproj | 2 +- .../CachingServiceTests.cs | 50 +++++++++++++++++++ .../LazyCache.UnitTestsNet50.csproj | 2 +- LazyCache/CachingService.cs | 4 +- 11 files changed, 257 insertions(+), 7 deletions(-) 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.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj b/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj index 5605db2..2dcfc2b 100644 --- a/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj +++ b/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj @@ -1,7 +1,7 @@ - netcoreapp2.1 + net6.0 false 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.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj b/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj index 3903a11..5f33f6f 100644 --- a/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj +++ b/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj @@ -1,7 +1,7 @@ - netcoreapp2.2 + net6.0 false 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.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj b/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj index 4b239ec..f626ed6 100644 --- a/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj +++ b/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj @@ -1,7 +1,7 @@ - netcoreapp3.0 + net6.0 false 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.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj b/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj index f14df3a..e82e615 100644 --- a/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj +++ b/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net6.0 false 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.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj b/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj index 40d55bb..eeee3a1 100644 --- a/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj +++ b/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj @@ -1,7 +1,7 @@ - net5.0 + net6.0 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, From 7ed89c1305ec9b6ccf5edcb0927ffba437a18014 Mon Sep 17 00:00:00 2001 From: touchofevil-dev <67504725+touchofevil-dev@users.noreply.github.com> Date: Fri, 12 May 2023 21:51:43 +0530 Subject: [PATCH 2/3] Reverted accidental commit to framework version --- LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj | 2 +- LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj | 2 +- LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj | 2 +- LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj | 2 +- LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj b/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj index 2dcfc2b..9348213 100644 --- a/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj +++ b/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj @@ -1,7 +1,7 @@ - net6.0 + netcoreapp2.1 false diff --git a/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj b/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj index 5f33f6f..9272c44 100644 --- a/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj +++ b/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj @@ -1,7 +1,7 @@ - net6.0 + netcoreapp2.2 false diff --git a/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj b/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj index f626ed6..2e827d0 100644 --- a/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj +++ b/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj @@ -1,7 +1,7 @@ - net6.0 + netcoreapp3.0 false diff --git a/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj b/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj index e82e615..26b2353 100644 --- a/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj +++ b/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj @@ -1,7 +1,7 @@ - net6.0 + netcoreapp3.1 false diff --git a/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj b/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj index eeee3a1..2eb7a9b 100644 --- a/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj +++ b/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj @@ -1,7 +1,7 @@ - net6.0 + net5.0 From e87d6127a79a0cfbc5f72c5ffe9fc67734554f73 Mon Sep 17 00:00:00 2001 From: touchofevil-dev <67504725+touchofevil-dev@users.noreply.github.com> Date: Fri, 12 May 2023 21:54:56 +0530 Subject: [PATCH 3/3] Hopefully Fixed alignment causing project file changes --- LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj | 4 ++-- LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj | 4 ++-- LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj | 4 ++-- LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj | 4 ++-- LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj b/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj index 9348213..58e4df9 100644 --- a/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj +++ b/LazyCache.UnitTestsCore21/LazyCache.UnitTestsCore21.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.1 + netcoreapp2.1 false diff --git a/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj b/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj index 9272c44..507600a 100644 --- a/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj +++ b/LazyCache.UnitTestsCore22/LazyCache.UnitTestsCore22.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.2 + netcoreapp2.2 false diff --git a/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj b/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj index 2e827d0..5c6118d 100644 --- a/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj +++ b/LazyCache.UnitTestsCore30/LazyCache.UnitTestsCore30.csproj @@ -1,7 +1,7 @@ - + - netcoreapp3.0 + netcoreapp3.0 false diff --git a/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj b/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj index 26b2353..0e76863 100644 --- a/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj +++ b/LazyCache.UnitTestsCore31/LazyCache.UnitTestsCore31.csproj @@ -1,7 +1,7 @@ - + - netcoreapp3.1 + netcoreapp3.1 false diff --git a/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj b/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj index 2eb7a9b..02b7a31 100644 --- a/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj +++ b/LazyCache.UnitTestsNet50/LazyCache.UnitTestsNet50.csproj @@ -1,7 +1,7 @@ - + - net5.0 + net5.0