From ac8c9da1e8028078189a44cf81a73d9f5d1df8c3 Mon Sep 17 00:00:00 2001 From: phadtrapong Date: Sat, 7 May 2022 10:38:02 +0700 Subject: [PATCH 1/3] Change TryGetValue Caching Service & Tests --- .../CachingServiceMemoryCacheProviderTests.cs | 13 +++++++++++++ LazyCache/CachingService.cs | 12 +++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs b/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs index a346ae3..0657b63 100644 --- a/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs +++ b/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs @@ -1169,5 +1169,18 @@ public void TryGetReturnsCachedValueAndTrue() Assert.Throws(() => sut.TryGetValue(key, out var value3)); } + + [Test] + public void TryGetReturnsCachedValueFromGetOrAdd() + { + sut.GetOrAdd(TestKey, () => testObject); + var isSuccess = sut.TryGetValue(TestKey, out var actual); + var expected = testObject; + + Assert.IsTrue(isSuccess); + Assert.IsNotNull(actual); + Assert.AreEqual(expected, actual); + } + } } \ No newline at end of file diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index df091de..c2412ed 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -92,7 +92,17 @@ public virtual bool TryGetValue(string key, out T value) { ValidateKey(key); - return CacheProvider.TryGetValue(key, out value); + var isGetSuccess = CacheProvider.TryGetValue>(key, out var lazyItemFactory); + + if (!isGetSuccess) + { + value = default(T); + return isGetSuccess; + } + + value = GetValueFromLazy(lazyItemFactory, out _); + + return isGetSuccess; } public virtual T GetOrAdd(string key, Func addItemFactory) From 9056dcf00e1619045e6579a9be6f20d36c2ed47c Mon Sep 17 00:00:00 2001 From: phadtrapong Date: Sat, 7 May 2022 11:17:16 +0700 Subject: [PATCH 2/3] Fix logic for normal cache add value --- LazyCache/CachingService.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index c2412ed..b6d5d4b 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -92,17 +92,22 @@ public virtual bool TryGetValue(string key, out T value) { ValidateKey(key); - var isGetSuccess = CacheProvider.TryGetValue>(key, out var lazyItemFactory); - - if (!isGetSuccess) + try + { + var item = CacheProvider.Get(key); + value = GetValueFromLazy(item, out var valueHasChangedType); + if (valueHasChangedType) + { + throw new InvalidCastException(); + } + } + catch (Exception ex) when (!(ex is InvalidCastException)) { value = default(T); - return isGetSuccess; + return false; } - value = GetValueFromLazy(lazyItemFactory, out _); - - return isGetSuccess; + return value is T; } public virtual T GetOrAdd(string key, Func addItemFactory) From 7be5ab847edb9cd8028c8c64ee6720c6c577a39d Mon Sep 17 00:00:00 2001 From: phadtrapong Date: Sat, 7 May 2022 11:58:16 +0700 Subject: [PATCH 3/3] Fix TryGetvalue logic --- LazyCache/CachingService.cs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index b6d5d4b..1695572 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -94,20 +94,14 @@ public virtual bool TryGetValue(string key, out T value) try { - var item = CacheProvider.Get(key); - value = GetValueFromLazy(item, out var valueHasChangedType); - if (valueHasChangedType) - { - throw new InvalidCastException(); - } + var contains = CacheProvider.TryGetValue>(key, out var lazyFactory); + value = GetValueFromLazy(lazyFactory, out var _); + return contains; } - catch (Exception ex) when (!(ex is InvalidCastException)) + catch (InvalidCastException) { - value = default(T); - return false; + return CacheProvider.TryGetValue(key, out value); } - - return value is T; } public virtual T GetOrAdd(string key, Func addItemFactory)