Skip to content

Commit

Permalink
Improve cache logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Dec 5, 2023
1 parent eb58975 commit 87c3836
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/handler/Cache/Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>GarageGroup.Infra</RootNamespace>
<AssemblyName>GarageGroup.Infra.Http.Cache</AssemblyName>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
40 changes: 20 additions & 20 deletions src/handler/Cache/InMemoryCacheHttpHandler/Handler.Send.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,46 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
{
Debug.Assert(request is not null);

if (request.Method != HttpMethod.Get && request.Method != HttpMethod.Head)
{
return base.SendAsync(request, cancellationToken);
}

if (option.ExpirationPerHttpResponseCode?.Count is not > 0)
{
return base.SendAsync(request, cancellationToken);
}

return InnerSendAsync(request, cancellationToken);
return InnerSendWithCacheAsync(request, cancellationToken);
}

private async Task<HttpResponseMessage> InnerSendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
private async Task<HttpResponseMessage> InnerSendWithCacheAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var requestMethod = request.Method;
var requestUri = request.RequestUri;

var key = CacheKeysProvider.GetKey(request);
if (requestMethod == HttpMethod.Get || requestMethod == HttpMethod.Head)
var cacheData = TryGetCacheData(key);

if (cacheData is not null)
{
var cacheData = TryGetCacheData(key);
if (cacheData is not null)
{
logger?.LogInformation("Cached response for {requestMethod} {requestUri}", requestMethod.Method, requestUri);
return request.PrepareCachedEntry(cacheData);
}
logger?.LogInformation("Cached response for {requestMethod} {requestUri}", requestMethod.Method, requestUri);
return request.PrepareCachedEntry(cacheData);
}

var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (requestMethod == HttpMethod.Get || requestMethod == HttpMethod.Head)
var absoluteExpirationRelativeToNow = response.StatusCode.GetAbsoluteExpirationRelativeToNow(option.ExpirationPerHttpResponseCode);
if (absoluteExpirationRelativeToNow != TimeSpan.Zero)
{
var absoluteExpirationRelativeToNow = response.StatusCode.GetAbsoluteExpirationRelativeToNow(option.ExpirationPerHttpResponseCode);
if (absoluteExpirationRelativeToNow != TimeSpan.Zero)
{
var entry = await response.ToCacheEntry();
TrySetCacheData(key, entry, absoluteExpirationRelativeToNow);

logger?.LogInformation("Cache response for {timeSpan}", absoluteExpirationRelativeToNow);
return request.PrepareCachedEntry(entry);
}
return response;
}

return response;
var entry = await response.ToCacheEntry();
TrySetCacheData(key, entry, absoluteExpirationRelativeToNow);

logger?.LogInformation("Cache response for {timeSpan}", absoluteExpirationRelativeToNow);
return request.PrepareCachedEntry(entry);
}

private static CacheData? TryGetCacheData(string key)
Expand Down

0 comments on commit 87c3836

Please sign in to comment.