Skip to content

Commit 37ced0e

Browse files
author
Thomas Galliker
committed
Code cleanup
1 parent 4b9d120 commit 37ced0e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

HttpClient.Caching/InMemory/InMemoryCacheHandler.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class InMemoryCacheHandler : DelegatingHandler
2222
/// If the key is present and the value is false, the cache will not be checked.
2323
/// If the key is present and the value is true, the cache will be checked.
2424
/// </summary>
25-
public readonly static HttpRequestOptionsKey<bool> UseCache = new(nameof(UseCache));
25+
public static readonly HttpRequestOptionsKey<bool> UseCache = new HttpRequestOptionsKey<bool>(nameof(UseCache));
2626
#else
2727
/// <summary>
2828
/// The key to use to store the UseCache value in the HttpRequestMessage.Properties dictionary.
@@ -34,7 +34,7 @@ public class InMemoryCacheHandler : DelegatingHandler
3434
public const string UseCache = nameof(UseCache);
3535
#endif
3636

37-
private static HashSet<HttpMethod> CachedHttpMethods = new HashSet<HttpMethod>
37+
private static readonly HashSet<HttpMethod> CachedHttpMethods = new HashSet<HttpMethod>
3838
{
3939
HttpMethod.Get,
4040
HttpMethod.Head
@@ -129,13 +129,12 @@ public void InvalidateCache(Uri uri, HttpMethod httpMethod = null)
129129
/// </summary>
130130
/// <param name="request"></param>
131131
/// <returns>A bool representing if the cache should be cached or not</returns>
132-
private bool ShouldTheCacheBeChecked(HttpRequestMessage request)
132+
private static bool ShouldTheCacheBeChecked(HttpRequestMessage request)
133133
{
134-
bool useCacheOption;
135134
#if NET5_0_OR_GREATER
136-
useCacheOption = request.Options.TryGetValue(UseCache, out bool useCache) == false || useCache == true;
135+
var useCacheOption = request.Options.TryGetValue(UseCache, out var useCache) == false || useCache == true;
137136
#else
138-
useCacheOption = request.Properties.TryGetValue(UseCache, out var useCache) == false || (bool)useCache == true;
137+
var useCacheOption = request.Properties.TryGetValue(UseCache, out var useCache) == false || (bool)useCache == true;
139138
#endif
140139

141140
return useCacheOption && request.Headers.CacheControl?.NoCache != true;
@@ -146,7 +145,7 @@ private bool ShouldTheCacheBeChecked(HttpRequestMessage request)
146145
/// </summary>
147146
/// <param name="response"></param>
148147
/// <returns>A bool representing if the response should be cached or not</returns>
149-
private bool ShouldCacheResponse(HttpResponseMessage response)
148+
private static bool ShouldCacheResponse(HttpResponseMessage response)
150149
{
151150
if (response.Headers.CacheControl is not null)
152151
{
@@ -169,7 +168,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
169168
// Gets the data from cache, and returns the data if it's a cache hit
170169
var isCachedHttpMethod = CachedHttpMethods.Contains(request.Method);
171170
// Check if the cache should be checked
172-
var shouldCheckCache = this.ShouldTheCacheBeChecked(request);
171+
var shouldCheckCache = ShouldTheCacheBeChecked(request);
173172
if (shouldCheckCache && isCachedHttpMethod)
174173
{
175174
key = this.CacheKeysProvider.GetKey(request);
@@ -194,7 +193,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
194193

195194
this.StatsProvider.ReportCacheMiss(response.StatusCode);
196195

197-
if (this.ShouldCacheResponse(response) && TimeSpan.Zero != maxCacheTime)
196+
if (ShouldCacheResponse(response) && TimeSpan.Zero != maxCacheTime)
198197
{
199198
var entry = await response.ToCacheEntryAsync();
200199
await this.responseCache.TrySetAsync(key, entry, maxCacheTime);
@@ -213,8 +212,9 @@ protected override HttpResponseMessage Send(HttpRequestMessage request, Cancella
213212

214213
// Gets the data from cache, and returns the data if it's a cache hit
215214
var isCachedHttpMethod = CachedHttpMethods.Contains(request.Method);
215+
216216
// Check if the cache should be checked
217-
var shouldCheckCache = this.ShouldTheCacheBeChecked(request);
217+
var shouldCheckCache = ShouldTheCacheBeChecked(request);
218218
if (shouldCheckCache && isCachedHttpMethod)
219219
{
220220
key = this.CacheKeysProvider.GetKey(request);
@@ -238,7 +238,7 @@ protected override HttpResponseMessage Send(HttpRequestMessage request, Cancella
238238

239239
this.StatsProvider.ReportCacheMiss(response.StatusCode);
240240

241-
if (this.ShouldCacheResponse(response) && TimeSpan.Zero != maxCacheTime)
241+
if (ShouldCacheResponse(response) && TimeSpan.Zero != maxCacheTime)
242242
{
243243
var cacheData = response.ToCacheEntry();
244244
this.responseCache.TrySetCacheData(key, cacheData, maxCacheTime);

0 commit comments

Comments
 (0)