From c82062ee013b58404c555782774096259d9a618e Mon Sep 17 00:00:00 2001 From: Andrew Beaven Date: Tue, 23 Feb 2021 16:04:54 +1300 Subject: [PATCH 1/2] Policy should be supplied when refreshing the cache due to type mismatch --- LazyCache/CachingService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index 30b6754..7e64767 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -137,7 +137,7 @@ object CacheFactory(ICacheEntry entry) => try { - cacheItem = CacheProvider.GetOrCreate(key, CacheFactory); + cacheItem = CacheProvider.GetOrCreate(key, policy, CacheFactory); } finally { @@ -226,7 +226,7 @@ object CacheFactory(ICacheEntry entry) => try { - cacheItem = CacheProvider.GetOrCreate(key, CacheFactory); + cacheItem = CacheProvider.GetOrCreate(key, policy, CacheFactory); } finally { From 7f960b275f3c528fe58421c9399377fa3a23f34a Mon Sep 17 00:00:00 2001 From: Andrew Beaven Date: Tue, 11 May 2021 11:40:40 +1200 Subject: [PATCH 2/2] Added tests to ensure policy is passed through when replacing cached item if type is changed --- .../CachingServiceMemoryCacheProviderTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs b/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs index 42aa3b9..7ca4192 100644 --- a/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs +++ b/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs @@ -263,6 +263,15 @@ public void GetOrAddAndThenGetOrAddDifferentTypeDoesLastInWins() Assert.IsInstanceOf(second); } + [Test] + public void GetOrAddAndThenGetOrAddDifferentTypeSuppliesPolicy() + { + sut.GetOrAdd(TestKey, cacheEntry => 123); + sut.GetOrAdd(TestKey, cacheEntry => "itemWithDifferentType", new MemoryCacheEntryOptions { SlidingExpiration = new TimeSpan(750) }); + Thread.Sleep(1500); + Assert.IsNull(sut.Get(TestKey)); + } + [Test] public void GetOrAddAndThenGetValueObjectReturnsCorrectType() { @@ -337,6 +346,15 @@ public async Task GetOrAddAsyncAndThenGetOrAddAsyncDifferentTypeDoesLastInWins() Assert.IsInstanceOf(second); } + [Test] + public async Task GetOrAddAsyncAndThenGetOrAddAsyncDifferentTypeSuppliesPolicy() + { + await sut.GetOrAddAsync(TestKey, cacheEntry => Task.FromResult(new object())); + await sut.GetOrAddAsync(TestKey, cacheEntry => Task.FromResult("itemWithDifferentType"), new MemoryCacheEntryOptions { SlidingExpiration = new TimeSpan(750) }); + await Task.Delay(1500); + Assert.IsNull(sut.Get(TestKey)); + } + [Test] public async Task GetOrAddAsyncAndThenGetAsyncWrongObjectReturnsNull() {