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

Commit 98d7022

Browse files
Merge pull request #18 from thefringeninja/cache-control
Fix Cache Control
2 parents 1217022 + 2cdf459 commit 98d7022

10 files changed

Lines changed: 68 additions & 29 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
@@ -4,7 +4,7 @@
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
<RuntimeIdentifiers>alpine.3.7-x64</RuntimeIdentifiers>
66
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
7-
<LangVersion>7.3</LangVersion>
7+
<LangVersion>latest</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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<DebugSymbols>true</DebugSymbols>
1515
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
1616
<NoWarn>1591</NoWarn>
17-
<LangVersion>7.3</LangVersion>
17+
<LangVersion>latest</LangVersion>
1818
</PropertyGroup>
1919
<ItemGroup>
2020
<ProjectReference Include="..\SqlStreamStore.HAL\SqlStreamStore.HAL.csproj" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace SqlStreamStore.HAL
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.Extensions.Primitives;
7+
8+
internal struct CacheControl : IEquatable<CacheControl>
9+
{
10+
private readonly string[] _values;
11+
12+
public static readonly CacheControl NoCache = new CacheControl(
13+
"max-age=0",
14+
"no-cache",
15+
"must-revalidate");
16+
17+
public static readonly CacheControl OneYear = new CacheControl("max-age=31536000");
18+
19+
private CacheControl(params string[] values)
20+
{
21+
if(values == null)
22+
throw new ArgumentNullException(nameof(values));
23+
_values = values;
24+
}
25+
26+
public bool Equals(CacheControl other) => _values
27+
.OrderBy(value => value)
28+
.SequenceEqual(other._values.OrderBy(value => value), StringComparer.OrdinalIgnoreCase);
29+
30+
public override bool Equals(object obj) => obj is CacheControl other && Equals(other);
31+
32+
public override int GetHashCode() => _values
33+
.Aggregate(397, (previous, value) => previous ^ (value.GetHashCode() * 397));
34+
35+
public static bool operator ==(CacheControl left, CacheControl right) => left.Equals(right);
36+
public static bool operator !=(CacheControl left, CacheControl right) => !left.Equals(right);
37+
public static implicit operator string[](CacheControl cacheControl) => cacheControl._values;
38+
public static implicit operator StringValues(CacheControl cacheControl) =>
39+
new StringValues(cacheControl._values);
40+
public static implicit operator KeyValuePair<string, string[]>(CacheControl cacheControl)
41+
=> new KeyValuePair<string, string[]>(Constants.Headers.CacheControl, cacheControl._values);
42+
43+
}
44+
}

src/SqlStreamStore.HAL/ETag.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
namespace SqlStreamStore.HAL
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using Microsoft.Extensions.Primitives;
46

57
internal struct ETag : IEquatable<ETag>
68
{
79
private readonly string _value;
8-
10+
911
public static ETag FromPosition(long position) => new ETag($@"""{position}""");
1012
public static ETag FromStreamVersion(int streamVersion) => new ETag($@"""{streamVersion}""");
1113
public static readonly ETag None = default;
12-
14+
1315
private ETag(string value)
1416
{
1517
if(value == null)
1618
{
1719
throw new ArgumentNullException(nameof(value));
1820
}
21+
1922
if(value[0] != '"' || value[value.Length - 1] != '"')
2023
{
2124
throw new ArgumentException("ETags bust be enclosed in double quotes.", nameof(value));
@@ -31,5 +34,9 @@ private ETag(string value)
3134
public static bool operator !=(ETag left, ETag right) => !left.Equals(right);
3235
public static implicit operator string(ETag etag) => etag._value;
3336
public static implicit operator string[](ETag etag) => new[] { etag._value };
37+
public static implicit operator StringValues(ETag etag) => new StringValues(etag._value);
38+
39+
public static implicit operator KeyValuePair<string, string[]>(ETag etag)
40+
=> new KeyValuePair<string, string[]>(Constants.Headers.ETag, new[] { etag._value });
3441
}
3542
}

src/SqlStreamStore.HAL/Resources/AllStreamResource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ public async Task<Response> Get(
9191

9292
if(page.TryGetETag(operation.FromPositionInclusive, out var eTag))
9393
{
94-
response.Headers[Constants.Headers.ETag] = eTag;
94+
response.Headers.Add(eTag);
95+
response.Headers.Add(CacheControl.NoCache);
96+
}
97+
else
98+
{
99+
response.Headers.Add(CacheControl.OneYear);
95100
}
96101

97102
return response;

src/SqlStreamStore.HAL/Resources/StreamMessageResource.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public async Task<Response> Get(
8585
{
8686
Headers =
8787
{
88-
[Constants.Headers.ETag] = eTag
88+
eTag,
89+
CacheControl.OneYear
8990
}
9091
};
9192
}
@@ -170,7 +171,7 @@ public static Link Last(ReadStreamPage page, ReadStreamOperation operation)
170171

171172
public static IEnumerable<Link> Navigation(
172173
ReadStreamMessageByStreamVersionOperation operation,
173-
StreamMessage message = default(StreamMessage))
174+
StreamMessage message = default)
174175
{
175176
yield return First();
176177

@@ -179,7 +180,7 @@ public static IEnumerable<Link> Navigation(
179180
yield return Previous(operation);
180181
}
181182

182-
if(message.MessageId != default(Guid))
183+
if(message.MessageId != default)
183184
{
184185
yield return Next(operation);
185186
}

src/SqlStreamStore.HAL/Resources/StreamMetadataResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<Response> Get(
5050
{
5151
Headers =
5252
{
53-
[Constants.Headers.ETag] = ETag.FromStreamVersion(result.MetadataStreamVersion)
53+
ETag.FromStreamVersion(result.MetadataStreamVersion)
5454
}
5555
};
5656

src/SqlStreamStore.HAL/Resources/StreamResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public async Task<Response> Get(ReadStreamOperation operation, CancellationToken
115115

116116
if (page.TryGetETag(out var eTag))
117117
{
118-
response.Headers[Constants.Headers.ETag] = eTag;
118+
response.Headers.Add(eTag);
119119
}
120120

121121
return response;

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>7.3</LangVersion>
9+
<LangVersion>latest</LangVersion>
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<PackageReference Include="Halcyon" Version="2.5.1" />

src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,6 @@ private static MidFunc MethodsNotAllowed(params string[] methods) => (context, n
7979
}
8080
};
8181

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-
9982
public static IApplicationBuilder UseSqlStreamStoreHal(
10083
this IApplicationBuilder builder,
10184
IStreamStore streamStore)
@@ -110,7 +93,6 @@ public static IApplicationBuilder UseSqlStreamStoreHal(
11093
.Use(CaseSensitiveQueryStrings)
11194
.Use(AcceptHalJson)
11295
.Use(HeadRequests)
113-
.Use(CacheControl)
11496
.UseIndex()
11597
.Map("/stream", UseAllStream(streamStore))
11698
.Map("/streams", UseStream(streamStore));

0 commit comments

Comments
 (0)