From b6e6f74aebaba9ab7218723e594be7ed742c5777 Mon Sep 17 00:00:00 2001 From: Nic Gamble Date: Wed, 1 Sep 2021 09:45:09 +0100 Subject: [PATCH] Use Generic type rather than object (#163) --- LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs | 2 ++ LazyCache/CachingService.cs | 2 +- LazyCache/IAppCache.cs | 2 +- LazyCache/ICacheProvider.cs | 2 +- LazyCache/Mocks/MockCacheProvider.cs | 2 +- LazyCache/Mocks/MockCachingService.cs | 2 +- LazyCache/Providers/MemoryCacheProvider.cs | 2 +- 7 files changed, 8 insertions(+), 6 deletions(-) diff --git a/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs b/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs index f381820..3de5c05 100644 --- a/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs +++ b/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs @@ -1124,6 +1124,8 @@ public void TryGetReturnsCachedValueAndTrue() var contains2 = sut.TryGetValue("invalidkey", out var value2); Assert.IsFalse(contains2); + + Assert.Throws(() => sut.TryGetValue(key, out var value3)); } } } \ No newline at end of file diff --git a/LazyCache/CachingService.cs b/LazyCache/CachingService.cs index 6becdd3..8eafe70 100644 --- a/LazyCache/CachingService.cs +++ b/LazyCache/CachingService.cs @@ -89,7 +89,7 @@ public virtual Task GetAsync(string key) return GetValueFromAsyncLazy(item, out _); } - public virtual bool TryGetValue(string key, out object value) + public virtual bool TryGetValue(string key, out T value) { ValidateKey(key); diff --git a/LazyCache/IAppCache.cs b/LazyCache/IAppCache.cs index 3d9133d..1bf9737 100644 --- a/LazyCache/IAppCache.cs +++ b/LazyCache/IAppCache.cs @@ -15,7 +15,7 @@ public interface IAppCache void Add(string key, T item, MemoryCacheEntryOptions policy); T Get(string key); Task GetAsync(string key); - bool TryGetValue(string key, out object value); + bool TryGetValue(string key, out T value); T GetOrAdd(string key, Func addItemFactory); T GetOrAdd(string key, Func addItemFactory, MemoryCacheEntryOptions policy); diff --git a/LazyCache/ICacheProvider.cs b/LazyCache/ICacheProvider.cs index 7a3ca0b..d96c257 100644 --- a/LazyCache/ICacheProvider.cs +++ b/LazyCache/ICacheProvider.cs @@ -12,6 +12,6 @@ public interface ICacheProvider : IDisposable object GetOrCreate(string key, MemoryCacheEntryOptions policy, Func func); void Remove(string key); Task GetOrCreateAsync(string key, Func> func); - bool TryGetValue(object key, out object value); + bool TryGetValue(object key, out T value); } } \ No newline at end of file diff --git a/LazyCache/Mocks/MockCacheProvider.cs b/LazyCache/Mocks/MockCacheProvider.cs index 46bbeff..019eaf4 100644 --- a/LazyCache/Mocks/MockCacheProvider.cs +++ b/LazyCache/Mocks/MockCacheProvider.cs @@ -35,7 +35,7 @@ public Task GetOrCreateAsync(string key, Func> func) return func(null); } - public bool TryGetValue(object key, out object value) + public bool TryGetValue(object key, out T value) { throw new NotImplementedException(); } diff --git a/LazyCache/Mocks/MockCachingService.cs b/LazyCache/Mocks/MockCachingService.cs index 9c9be86..4e9076d 100644 --- a/LazyCache/Mocks/MockCachingService.cs +++ b/LazyCache/Mocks/MockCachingService.cs @@ -52,7 +52,7 @@ public void Add(string key, T item, MemoryCacheEntryOptions policy) { } - public bool TryGetValue(string key, out object value) + public bool TryGetValue(string key, out T value) { value = default(T); return true; diff --git a/LazyCache/Providers/MemoryCacheProvider.cs b/LazyCache/Providers/MemoryCacheProvider.cs index 72739db..bbee381 100644 --- a/LazyCache/Providers/MemoryCacheProvider.cs +++ b/LazyCache/Providers/MemoryCacheProvider.cs @@ -78,7 +78,7 @@ public Task GetOrCreateAsync(string key, Func> factor return cache.GetOrCreateAsync(key, factory); } - public bool TryGetValue(object key, out object value) + public bool TryGetValue(object key, out T value) { return cache.TryGetValue(key, out value); }