Skip to content

Commit

Permalink
Set CacheControl via typed response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Apr 13, 2024
1 parent caac508 commit 9729439
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json;
using HotChocolate.Execution.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
#if !NET6_0_OR_GREATER
using Microsoft.Net.Http.Headers;
#endif
Expand Down Expand Up @@ -168,10 +169,10 @@ public async ValueTask FormatAsync(
response.StatusCode = statusCode;

if (result.ContextData is not null &&
result.ContextData.TryGetValue(CacheControlHeaderValue, out var value) &&
value is string cacheControlHeaderValue)
result.ContextData.TryGetValue(WellKnownContextData.CacheControlHeaderValue, out var value) &&
value is CacheControlHeaderValue cacheControlHeaderValue)
{
response.Headers.CacheControl = cacheControlHeaderValue;
response.GetTypedHeaders().CacheControl = cacheControlHeaderValue;
}

OnWriteResponseHeaders(queryResult, format, response.Headers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
Expand All @@ -13,10 +15,6 @@ namespace HotChocolate.Caching;
/// </summary>
internal sealed class CacheControlConstraintsOptimizer : IOperationOptimizer
{
private const string _cacheControlValueTemplate = "{0}, max-age={1}";
private const string _cacheControlPrivateScope = "private";
private const string _cacheControlPublicScope = "public";

public void OptimizeOperation(OperationOptimizerContext context)
{
if (context.Definition.Operation is not OperationType.Query ||
Expand All @@ -31,18 +29,12 @@ public void OptimizeOperation(OperationOptimizerContext context)

if (constraints.MaxAge is not null)
{
var cacheType = constraints.Scope switch
var headerValue = new CacheControlHeaderValue
{
CacheControlScope.Private => _cacheControlPrivateScope,
CacheControlScope.Public => _cacheControlPublicScope,
_ => throw ThrowHelper.UnexpectedCacheControlScopeValue(constraints.Scope),
Private = constraints.Scope == CacheControlScope.Private,
MaxAge = TimeSpan.FromSeconds(constraints.MaxAge.Value),
};

var headerValue = string.Format(
_cacheControlValueTemplate,
cacheType,
constraints.MaxAge);

context.ContextData.Add(
WellKnownContextData.CacheControlConstraints,
new ImmutableCacheConstraints(
Expand Down
7 changes: 4 additions & 3 deletions src/HotChocolate/Caching/src/Caching/QueryCacheMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using HotChocolate.Execution;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -30,8 +31,8 @@ public async ValueTask InvokeAsync(IRequestContext context)
}

if (context.Operation?.ContextData is null ||
!context.Operation.ContextData.TryGetValue(CacheControlHeaderValue, out var value) ||
value is not string cacheControlHeaderValue)
!context.Operation.ContextData.TryGetValue(WellKnownContextData.CacheControlHeaderValue, out var value) ||
value is not CacheControlHeaderValue cacheControlHeaderValue)
{
return;
}
Expand All @@ -45,7 +46,7 @@ queryResult.ContextData is not null
? new ExtensionData(queryResult.ContextData)
: new ExtensionData();

contextData.Add(CacheControlHeaderValue, cacheControlHeaderValue);
contextData.Add(WellKnownContextData.CacheControlHeaderValue, cacheControlHeaderValue);

context.Result = new QueryResult(
data: queryResult.Data,
Expand Down

0 comments on commit 9729439

Please sign in to comment.