Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit b24f9c6

Browse files
might as well add cache control
1 parent fa0a786 commit b24f9c6

6 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/SqlStreamStore.HAL.DevServer/SqlStreamStore.HAL.DevServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<LangVersion>latest</LangVersion>
65
<RuntimeIdentifiers>alpine.3.7-x64</RuntimeIdentifiers>
76
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
7+
<LangVersion>7.3</LangVersion>
88
</PropertyGroup>
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.1" />

src/SqlStreamStore.HAL.Tests/SqlStreamStore.HAL.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<DebugSymbols>true</DebugSymbols>
1515
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
1616
<NoWarn>1591</NoWarn>
17+
<LangVersion>7.3</LangVersion>
1718
</PropertyGroup>
1819
<ItemGroup>
1920
<ProjectReference Include="..\SqlStreamStore.HAL\SqlStreamStore.HAL.csproj" />

src/SqlStreamStore.HAL/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static class Headers
1010
public const string Location = "Location";
1111
public const string ETag = "ETag";
1212
public const string IfNoneMatch = "If-None-Match";
13+
public const string CacheControl = "Cache-Control";
1314

1415
public static class ContentTypes
1516
{

src/SqlStreamStore.HAL/Response.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ public Response(HALResponse hal, int statusCode = 200)
1414
{
1515
Hal = hal;
1616
StatusCode = statusCode;
17-
Headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
18-
{
19-
["Cache-Control"] = new[] { "max-age=0" }
20-
};
17+
Headers = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
2118
}
2219
}
2320
}

src/SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<AssemblyTitle>Stream Store - HAL Server</AssemblyTitle>
77
<DebugSymbols>true</DebugSymbols>
88
<NoWarn>1701;1702;1705;1591</NoWarn>
9-
<LangVersion>latest</LangVersion>
9+
<LangVersion>7.3</LangVersion>
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<PackageReference Include="Halcyon" Version="2.5.1" />

src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
public static class SqlStreamStoreHalMiddleware
1818
{
19+
private static readonly string[] s_NoCache =
20+
{
21+
"max-age=0",
22+
"no-cache",
23+
"must-revalidate"
24+
};
25+
26+
private static readonly string[] s_CacheForOneYear =
27+
{
28+
"max-age=31536000"
29+
};
30+
1931
private static MidFunc CaseSensitiveQueryStrings => (context, next) =>
2032
{
2133
if(context.Request.QueryString != QueryString.Empty)
@@ -67,6 +79,23 @@ private static MidFunc MethodsNotAllowed(params string[] methods) => (context, n
6779
}
6880
};
6981

82+
private static MidFunc CacheControl => (context, next) =>
83+
{
84+
Task AddCacheControlHeaders()
85+
{
86+
context.Response.Headers[Constants.Headers.CacheControl] =
87+
context.Response.Headers.ContainsKey(Constants.Headers.ETag)
88+
? s_NoCache
89+
: s_CacheForOneYear;
90+
91+
return Task.CompletedTask;
92+
}
93+
94+
context.Response.OnStarting(AddCacheControlHeaders);
95+
96+
return next();
97+
};
98+
7099
public static IApplicationBuilder UseSqlStreamStoreHal(
71100
this IApplicationBuilder builder,
72101
IStreamStore streamStore)
@@ -81,6 +110,7 @@ public static IApplicationBuilder UseSqlStreamStoreHal(
81110
.Use(CaseSensitiveQueryStrings)
82111
.Use(AcceptHalJson)
83112
.Use(HeadRequests)
113+
.Use(CacheControl)
84114
.UseIndex()
85115
.Map("/stream", UseAllStream(streamStore))
86116
.Map("/streams", UseStream(streamStore));

0 commit comments

Comments
 (0)