Skip to content

Better control of caching logic by following headers and a manual option to turn of caching for a request #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 80 additions & 11 deletions HttpClient.Caching/InMemory/InMemoryCacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Abstractions;
Expand All @@ -14,7 +15,27 @@ namespace Microsoft.Extensions.Caching.InMemory
/// </summary>
public class InMemoryCacheHandler : DelegatingHandler
{
private static HashSet<HttpMethod> CachedHttpMethods = new HashSet<HttpMethod>
#if NET5_0_OR_GREATER
/// <summary>
/// The key to use to store the UseCache value in the HttpRequestMessage.Options dictionary.
/// This key is used to determine if the cache should be checked for the request.
/// If the key is not present, the cache will be checked.
/// If the key is present and the value is false, the cache will not be checked.
/// If the key is present and the value is true, the cache will be checked.
/// </summary>
public static readonly HttpRequestOptionsKey<bool> UseCache = new HttpRequestOptionsKey<bool>(nameof(UseCache));
#else
/// <summary>
/// The key to use to store the UseCache value in the HttpRequestMessage.Properties dictionary.
/// This key is used to determine if the cache should be checked for the request.
/// If the key is not present, the cache will be checked.
/// If the key is present and the value is false, the cache will not be checked.
/// If the key is present and the value is true, the cache will be checked.
/// </summary>
public const string UseCache = nameof(UseCache);
#endif

private static readonly HashSet<HttpMethod> CachedHttpMethods = new HashSet<HttpMethod>
{
HttpMethod.Get,
HttpMethod.Head
Expand Down Expand Up @@ -50,10 +71,10 @@ public InMemoryCacheHandler(HttpMessageHandler innerHandler = null,
IStatsProvider statsProvider = null,
ICacheKeysProvider cacheKeysProvider = null)
: this(innerHandler,
cacheExpirationPerHttpResponseCode,
statsProvider,
new MemoryCache(new MemoryCacheOptions()),
cacheKeysProvider)
cacheExpirationPerHttpResponseCode,
statsProvider,
new MemoryCache(new MemoryCacheOptions()),
cacheKeysProvider)
{
}

Expand Down Expand Up @@ -104,6 +125,40 @@ public void InvalidateCache(Uri uri, HttpMethod httpMethod = null)
}
}

/// <summary>
/// Determines if the cache should be checked for the request.
/// </summary>
/// <param name="request"></param>
/// <returns>A bool representing if the cache should be cached or not</returns>
private static bool ShouldTheCacheBeChecked(HttpRequestMessage request)
{
#if NET5_0_OR_GREATER
var useCacheOption = request.Options.TryGetValue(UseCache, out var useCache) == false || useCache == true;
#else
var useCacheOption = request.Properties.TryGetValue(UseCache, out var useCache) == false || (bool)useCache == true;
#endif

return useCacheOption && request.Headers.CacheControl?.NoCache != true;
}

/// <summary>
/// Determines if the response should be cached.
/// </summary>
/// <param name="response"></param>
/// <returns>A bool representing if the response should be cached or not</returns>
private static bool ShouldCacheResponse(HttpResponseMessage response)
{
if (response.Headers.CacheControl is CacheControlHeaderValue cacheControl)
{
return
cacheControl.NoStore == false &&
cacheControl.NoCache == false &&
response.StatusCode != HttpStatusCode.NotModified;
}

return response.StatusCode != HttpStatusCode.NotModified;
}

/// <summary>
/// Tries to get the value from the cache, and only calls the delegating handler on cache misses.
/// </summary>
Expand All @@ -114,7 +169,10 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

// Gets the data from cache, and returns the data if it's a cache hit
var isCachedHttpMethod = CachedHttpMethods.Contains(request.Method);
if (isCachedHttpMethod)

// Check if the cache should be checked
var shouldCheckCache = ShouldTheCacheBeChecked(request);
if (shouldCheckCache && isCachedHttpMethod)
{
key = this.CacheKeysProvider.GetKey(request);

Expand All @@ -131,13 +189,17 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (isCachedHttpMethod)
{
var absoluteExpirationRelativeToNow = response.StatusCode.GetAbsoluteExpirationRelativeToNow(this.cacheExpirationPerHttpResponseCode);
var maxAgeHeader = response.Headers.CacheControl?.MaxAge ?? TimeSpan.MaxValue;

// If the server sends a Cache-Control header with a max-age that is less than the configured expiration time, use the max-age value
var maxCacheTime = (maxAgeHeader < absoluteExpirationRelativeToNow) ? maxAgeHeader : absoluteExpirationRelativeToNow;

this.StatsProvider.ReportCacheMiss(response.StatusCode);

if (TimeSpan.Zero != absoluteExpirationRelativeToNow)
if (ShouldCacheResponse(response) && TimeSpan.Zero != maxCacheTime)
{
var entry = await response.ToCacheEntryAsync();
await this.responseCache.TrySetAsync(key, entry, absoluteExpirationRelativeToNow);
await this.responseCache.TrySetAsync(key, entry, maxCacheTime);
return request.PrepareCachedEntry(entry);
}
}
Expand All @@ -153,7 +215,10 @@ protected override HttpResponseMessage Send(HttpRequestMessage request, Cancella

// Gets the data from cache, and returns the data if it's a cache hit
var isCachedHttpMethod = CachedHttpMethods.Contains(request.Method);
if (isCachedHttpMethod)

// Check if the cache should be checked
var shouldCheckCache = ShouldTheCacheBeChecked(request);
if (shouldCheckCache && isCachedHttpMethod)
{
key = this.CacheKeysProvider.GetKey(request);

Expand All @@ -169,13 +234,17 @@ protected override HttpResponseMessage Send(HttpRequestMessage request, Cancella
if (isCachedHttpMethod)
{
var absoluteExpirationRelativeToNow = response.StatusCode.GetAbsoluteExpirationRelativeToNow(this.cacheExpirationPerHttpResponseCode);
var maxAgeHeader = response.Headers.CacheControl?.MaxAge ?? TimeSpan.MaxValue;

// If the server sends a Cache-Control header with a max-age that is less than the configured expiration time, use the max-age value
var maxCacheTime = (maxAgeHeader < absoluteExpirationRelativeToNow) ? maxAgeHeader : absoluteExpirationRelativeToNow;

this.StatsProvider.ReportCacheMiss(response.StatusCode);

if (TimeSpan.Zero != absoluteExpirationRelativeToNow)
if (ShouldCacheResponse(response) && TimeSpan.Zero != maxCacheTime)
{
var cacheData = response.ToCacheEntry();
this.responseCache.TrySetCacheData(key, cacheData, absoluteExpirationRelativeToNow);
this.responseCache.TrySetCacheData(key, cacheData, maxCacheTime);
return request.PrepareCachedEntry(cacheData);
}
}
Expand Down
Loading