@@ -22,7 +22,7 @@ public class InMemoryCacheHandler : DelegatingHandler
22
22
/// If the key is present and the value is false, the cache will not be checked.
23
23
/// If the key is present and the value is true, the cache will be checked.
24
24
/// </summary>
25
- public readonly static HttpRequestOptionsKey < bool > UseCache = new ( nameof ( UseCache ) ) ;
25
+ public static readonly HttpRequestOptionsKey < bool > UseCache = new HttpRequestOptionsKey < bool > ( nameof ( UseCache ) ) ;
26
26
#else
27
27
/// <summary>
28
28
/// The key to use to store the UseCache value in the HttpRequestMessage.Properties dictionary.
@@ -34,7 +34,7 @@ public class InMemoryCacheHandler : DelegatingHandler
34
34
public const string UseCache = nameof ( UseCache ) ;
35
35
#endif
36
36
37
- private static HashSet < HttpMethod > CachedHttpMethods = new HashSet < HttpMethod >
37
+ private static readonly HashSet < HttpMethod > CachedHttpMethods = new HashSet < HttpMethod >
38
38
{
39
39
HttpMethod . Get ,
40
40
HttpMethod . Head
@@ -129,13 +129,12 @@ public void InvalidateCache(Uri uri, HttpMethod httpMethod = null)
129
129
/// </summary>
130
130
/// <param name="request"></param>
131
131
/// <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 )
133
133
{
134
- bool useCacheOption ;
135
134
#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 ;
137
136
#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 ;
139
138
#endif
140
139
141
140
return useCacheOption && request . Headers . CacheControl ? . NoCache != true ;
@@ -146,7 +145,7 @@ private bool ShouldTheCacheBeChecked(HttpRequestMessage request)
146
145
/// </summary>
147
146
/// <param name="response"></param>
148
147
/// <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 )
150
149
{
151
150
if ( response . Headers . CacheControl is not null )
152
151
{
@@ -169,7 +168,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
169
168
// Gets the data from cache, and returns the data if it's a cache hit
170
169
var isCachedHttpMethod = CachedHttpMethods . Contains ( request . Method ) ;
171
170
// Check if the cache should be checked
172
- var shouldCheckCache = this . ShouldTheCacheBeChecked ( request ) ;
171
+ var shouldCheckCache = ShouldTheCacheBeChecked ( request ) ;
173
172
if ( shouldCheckCache && isCachedHttpMethod )
174
173
{
175
174
key = this . CacheKeysProvider . GetKey ( request ) ;
@@ -194,7 +193,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
194
193
195
194
this . StatsProvider . ReportCacheMiss ( response . StatusCode ) ;
196
195
197
- if ( this . ShouldCacheResponse ( response ) && TimeSpan . Zero != maxCacheTime )
196
+ if ( ShouldCacheResponse ( response ) && TimeSpan . Zero != maxCacheTime )
198
197
{
199
198
var entry = await response . ToCacheEntryAsync ( ) ;
200
199
await this . responseCache . TrySetAsync ( key , entry , maxCacheTime ) ;
@@ -213,8 +212,9 @@ protected override HttpResponseMessage Send(HttpRequestMessage request, Cancella
213
212
214
213
// Gets the data from cache, and returns the data if it's a cache hit
215
214
var isCachedHttpMethod = CachedHttpMethods . Contains ( request . Method ) ;
215
+
216
216
// Check if the cache should be checked
217
- var shouldCheckCache = this . ShouldTheCacheBeChecked ( request ) ;
217
+ var shouldCheckCache = ShouldTheCacheBeChecked ( request ) ;
218
218
if ( shouldCheckCache && isCachedHttpMethod )
219
219
{
220
220
key = this . CacheKeysProvider . GetKey ( request ) ;
@@ -238,7 +238,7 @@ protected override HttpResponseMessage Send(HttpRequestMessage request, Cancella
238
238
239
239
this . StatsProvider . ReportCacheMiss ( response . StatusCode ) ;
240
240
241
- if ( this . ShouldCacheResponse ( response ) && TimeSpan . Zero != maxCacheTime )
241
+ if ( ShouldCacheResponse ( response ) && TimeSpan . Zero != maxCacheTime )
242
242
{
243
243
var cacheData = response . ToCacheEntry ( ) ;
244
244
this . responseCache . TrySetCacheData ( key , cacheData , maxCacheTime ) ;
0 commit comments