Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit 91b81c9

Browse files
committed
Update ResponseCache to handle VaryBy rules
1 parent 81223e1 commit 91b81c9

File tree

13 files changed

+241
-39
lines changed

13 files changed

+241
-39
lines changed

samples/ResponseCachingSample/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ public void ConfigureServices(IServiceCollection services)
2020

2121
public void Configure(IApplicationBuilder app)
2222
{
23+
app.UseDeveloperExceptionPage();
2324
app.UseResponseCaching();
2425
app.Run(async (context) =>
2526
{
27+
// These settings should be configured by context.Response.Cache.*
2628
context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue()
2729
{
2830
Public = true,
2931
MaxAge = TimeSpan.FromSeconds(10)
3032
};
33+
context.Response.GetTypedHeaders().Append("Vary", "Accept-Language");
34+
3135
await context.Response.WriteAsync("Hello World! " + DateTime.UtcNow);
3236
});
3337
}

samples/ResponseCachingSample/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"version": "1.1.0-*",
33
"dependencies": {
4+
"Microsoft.AspNetCore.Diagnostics": "0.1.0-*",
45
"Microsoft.AspNetCore.ResponseCaching": "0.1.0-*",
56
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-*",
67
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*",

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/ResponseCacheEntry.cs renamed to src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/CachedResponse.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55

66
namespace Microsoft.AspNetCore.ResponseCaching
77
{
8-
public class ResponseCacheEntry
8+
public class CachedResponse : IResponseCacheEntry
99
{
10+
public string Key { get; set; }
11+
12+
public ResponseCacheEntryType Type => ResponseCacheEntryType.Response;
13+
1014
public int StatusCode { get; set; }
15+
1116
internal IHeaderDictionary Headers { get; set; } = new HeaderDictionary();
17+
1218
internal byte[] Body { get; set; }
1319
}
1420
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.ResponseCaching
5+
{
6+
public class CachedVaryBy : IResponseCacheEntry
7+
{
8+
public string Key { get; set; }
9+
10+
public ResponseCacheEntryType Type => ResponseCacheEntryType.VaryBy;
11+
12+
public string[] Headers { get; set; }
13+
14+
public string[] Params { get; set; }
15+
}
16+
}

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/DefaultResponseCacheEntrySerializer.cs

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class DefaultResponseCacheSerializer
1111
{
1212
private const int FormatVersion = 1;
1313

14-
public static ResponseCacheEntry Deserialize(byte[] serializedEntry)
14+
public static IResponseCacheEntry Deserialize(byte[] serializedEntry)
1515
{
1616
using (var memory = new MemoryStream(serializedEntry))
1717
{
@@ -22,19 +22,20 @@ public static ResponseCacheEntry Deserialize(byte[] serializedEntry)
2222
}
2323
}
2424

25-
public static byte[] Serialize(ResponseCacheEntry entry)
25+
public static byte[] Serialize(IResponseCacheEntry entry)
2626
{
2727
using (var memory = new MemoryStream())
2828
{
2929
using (var writer = new BinaryWriter(memory))
3030
{
3131
Write(writer, entry);
32+
writer.Flush();
3233
return memory.ToArray();
3334
}
3435
}
3536
}
3637

37-
public static ResponseCacheEntry Read(BinaryReader reader)
38+
public static IResponseCacheEntry Read(BinaryReader reader)
3839
{
3940
if (reader == null)
4041
{
@@ -46,6 +47,28 @@ public static ResponseCacheEntry Read(BinaryReader reader)
4647
return null;
4748
}
4849

50+
var key = reader.ReadString();
51+
var type = (ResponseCacheEntryType)reader.ReadInt32();
52+
53+
if (type == ResponseCacheEntryType.Response)
54+
{
55+
var cachedResponse = ReadCachedResponse(reader);
56+
cachedResponse.Key = key;
57+
return cachedResponse;
58+
}
59+
else if (type == ResponseCacheEntryType.VaryBy)
60+
{
61+
var cachedResponse = ReadCachedVaryBy(reader);
62+
cachedResponse.Key = key;
63+
return cachedResponse;
64+
}
65+
66+
// Unable to read as CachedResponse or CachedVaryBy
67+
return null;
68+
}
69+
70+
private static IResponseCacheEntry ReadCachedResponse(BinaryReader reader)
71+
{
4972
var statusCode = reader.ReadInt32();
5073
var headerCount = reader.ReadInt32();
5174
var headers = new HeaderDictionary();
@@ -58,9 +81,31 @@ public static ResponseCacheEntry Read(BinaryReader reader)
5881
var bodyLength = reader.ReadInt32();
5982
var body = reader.ReadBytes(bodyLength);
6083

61-
return new ResponseCacheEntry { StatusCode = statusCode, Headers = headers, Body = body };
84+
return new CachedResponse { StatusCode = statusCode, Headers = headers, Body = body };
6285
}
63-
public static void Write(BinaryWriter writer, ResponseCacheEntry entry)
86+
87+
private static IResponseCacheEntry ReadCachedVaryBy(BinaryReader reader)
88+
{
89+
var headerCount = reader.ReadInt32();
90+
var headerArray = new string[headerCount];
91+
for (var index = 0; index < headerCount; index++)
92+
{
93+
var value = reader.ReadString();
94+
headerArray[index] = value;
95+
}
96+
97+
var paramCount = reader.ReadInt32();
98+
var paramArray = new string[paramCount];
99+
for (var index = 0; index < paramCount; index++)
100+
{
101+
var value = reader.ReadString();
102+
paramArray[index] = value;
103+
}
104+
105+
return new CachedVaryBy { Headers = headerArray, Params = paramArray };
106+
}
107+
108+
public static void Write(BinaryWriter writer, IResponseCacheEntry entry)
64109
{
65110
if (writer == null)
66111
{
@@ -73,9 +118,23 @@ public static void Write(BinaryWriter writer, ResponseCacheEntry entry)
73118
}
74119

75120
writer.Write(FormatVersion);
121+
writer.Write(entry.Key);
122+
writer.Write((int)entry.Type);
123+
124+
if (entry is CachedResponse)
125+
{
126+
WriteCachedResponse(writer, entry as CachedResponse);
127+
}
128+
else if (entry is CachedVaryBy)
129+
{
130+
WriteCachedVaryBy(writer, entry as CachedVaryBy);
131+
}
132+
}
133+
134+
private static void WriteCachedResponse(BinaryWriter writer, CachedResponse entry)
135+
{
76136
writer.Write(entry.StatusCode);
77137
writer.Write(entry.Headers.Count);
78-
79138
foreach (var header in entry.Headers)
80139
{
81140
writer.Write(header.Key);
@@ -85,5 +144,33 @@ public static void Write(BinaryWriter writer, ResponseCacheEntry entry)
85144
writer.Write(entry.Body.Length);
86145
writer.Write(entry.Body);
87146
}
147+
148+
private static void WriteCachedVaryBy(BinaryWriter writer, CachedVaryBy entry)
149+
{
150+
if (entry.Headers == null)
151+
{
152+
writer.Write(0);
153+
}
154+
else
155+
{
156+
writer.Write(entry.Headers.Length);
157+
foreach (var header in entry.Headers)
158+
{
159+
writer.Write(header);
160+
}
161+
}
162+
if (entry.Params == null)
163+
{
164+
writer.Write(0);
165+
}
166+
else
167+
{
168+
writer.Write(entry.Params.Length);
169+
foreach (var param in entry.Params)
170+
{
171+
writer.Write(param);
172+
}
173+
}
174+
}
88175
}
89176
}

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/DistributedResponseCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public DistributedResponseCache(IDistributedCache cache)
1414
_cache = cache;
1515
}
1616

17-
public ResponseCacheEntry Get(string key)
17+
public IResponseCacheEntry Get(string key)
1818
{
1919
var serializedEntry = _cache.Get(key);
2020
if (serializedEntry != null)
@@ -32,7 +32,7 @@ public void Remove(string key)
3232
_cache.Remove(key);
3333
}
3434

35-
public void Set(string key, ResponseCacheEntry entry)
35+
public void Set(string key, IResponseCacheEntry entry)
3636
{
3737
_cache.Set(key, DefaultResponseCacheSerializer.Serialize(entry));
3838
}

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/IResponseCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace Microsoft.AspNetCore.ResponseCaching
55
{
66
public interface IResponseCache
77
{
8-
ResponseCacheEntry Get(string key);
9-
void Set(string key, ResponseCacheEntry entry);
8+
IResponseCacheEntry Get(string key);
9+
void Set(string key, IResponseCacheEntry entry);
1010
void Remove(string key);
1111
}
1212
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.ResponseCaching
5+
{
6+
public interface IResponseCacheEntry
7+
{
8+
string Key { get; set; }
9+
ResponseCacheEntryType Type { get; }
10+
}
11+
}

src/Microsoft.AspNetCore.ResponseCaching/ResponseCache/MemoryResponseCache.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public MemoryResponseCache(IMemoryCache cache)
1414
_cache = cache;
1515
}
1616

17-
public ResponseCacheEntry Get(string key)
17+
public IResponseCacheEntry Get(string key)
1818
{
19-
return _cache.Get<ResponseCacheEntry>(key);
19+
return _cache.Get<IResponseCacheEntry>(key);
2020
}
2121

2222
public void Remove(string key)
2323
{
2424
_cache.Remove(key);
2525
}
2626

27-
public void Set(string key, ResponseCacheEntry entry)
27+
public void Set(string key, IResponseCacheEntry entry)
2828
{
2929
_cache.Set(key, entry);
3030
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.ResponseCaching
5+
{
6+
public enum ResponseCacheEntryType
7+
{
8+
Response = 0,
9+
VaryBy = 1
10+
}
11+
}

0 commit comments

Comments
 (0)