-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostResponseCacheMiddleware.cs
335 lines (312 loc) · 14.3 KB
/
PostResponseCacheMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.ResponseCaching;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using NetPro.RedisManager;
using NetPro.ShareRequestBody;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace NetPro.ResponseCache
{
public class PostResponseCacheMiddleware
{
private readonly ILogger _iLogger;
private readonly RequestDelegate _next;
private IMemoryCache _memorycache;
private ResponseCacheOption _responseCacheOption;
private readonly IConfiguration _configuration;
/// <summary>
///
/// </summary>
/// <param name="next"></param>
/// <param name="configuration"></param>
/// <param name="iLogger"></param>
/// <param name="memorycache"></param>
/// <param name="responseCacheOption"></param>
public PostResponseCacheMiddleware(RequestDelegate next, IConfiguration configuration,
ILogger<PostResponseCacheMiddleware> iLogger,
IMemoryCache memorycache,
ResponseCacheOption responseCacheOption)
{
_next = next;
_configuration = configuration;
_iLogger = iLogger;
_memorycache = memorycache;
_responseCacheOption = responseCacheOption;
}
/// <summary>
/// Post:(从头排序后+body json整体 )hash
/// </summary>
/// <param name="context"></param>
/// <param name="responseCacheData">自定义对象不能ctor注入</param>
/// <returns></returns>
public async Task InvokeAsync(HttpContext context, ResponseCacheData responseCacheData, RequestCacheData requestCacheData)
{
context.Request.EnableBuffering();
var token = context.RequestAborted.Register(async () =>
{
await Task.CompletedTask;
return;
});
var endpoint = context.Features.Get<IEndpointFeature>()?.Endpoint;
if (endpoint != null)
{
if (endpoint.Metadata
.Any(m => m is IgnorePostResponseCacheAttribute))
{
goto gotoNext;
}
}
if (context.Request.Method.Equals("get", StringComparison.OrdinalIgnoreCase)
|| context.Request.Method.Equals("head", StringComparison.OrdinalIgnoreCase)
|| _memorycache.TryGetValue($"PostResponseCache_{context.Request.Path}", out object _tempIgnoe)
|| _memorycache.TryGetValue($"IgnorePostResponseCache_{context.Request.Path}", out object _temp))
{
goto gotoNext;
}
else
{
var convertedDictionatry = context.Request.Query.ToDictionary(s => s.Key.ToLower(), s => s.Value);
foreach (var item in _responseCacheOption?.IgnoreVaryQuery ?? new List<string>())
{
if (convertedDictionatry.ContainsKey(item.ToLower()))
convertedDictionatry.Remove(item.ToLower());
}
StringBuilder requestStrKey = new StringBuilder(context.Request.Path);
foreach (var item in convertedDictionatry)
{
requestStrKey.Append($"{item.Key}{item.Value}");
}
string bodyValue;
if (requestCacheData == null || string.IsNullOrEmpty(requestCacheData.Body))
{
bodyValue = await Common.ReadAsString(context);
requestCacheData = new RequestCacheData { Body = bodyValue };
}
else
bodyValue = requestCacheData.Body;
if (!string.IsNullOrEmpty(bodyValue) && !"null".Equals(bodyValue))
{
//非Get请求body有值才被缓存,其他默认不缓存,防止body读取失败导致缓存异常
bodyValue = Regex.Replace(bodyValue, @"\s(?=([^""]*""[^""]*"")*[^""]*$)", string.Empty);
bodyValue = bodyValue.Replace("\r\n", "").Replace(" : ", ":").Replace("\n ", "").Replace("\n", "").Replace(": ", ":").Replace(", ", ",");
requestStrKey.Append($"body{bodyValue}");
ResponseCacheData cacheResponseBody = null;
IRedisManager _redisManager = null;
if (_responseCacheOption.Cluster)
{
_redisManager = context.RequestServices.GetService<IRedisManager>();
if (_redisManager == null)
{
throw new ArgumentNullException(nameof(RedisCacheOption), $"PostResponseCache组件在集群模式下未检测到NetPro.RedisManager配置节点{nameof(RedisCacheOption)}");
}
cacheResponseBody = _redisManager.Get<ResponseCacheData>($"NetProPostResponse:{requestStrKey}");
}
else
{
cacheResponseBody = _memorycache.Get<ResponseCacheData>($"NetProPostResponse:{requestStrKey}");
}
if (cacheResponseBody != null && !context.RequestAborted.IsCancellationRequested)
{
//https://stackoverflow.com/questions/45675102/asp-net-core-middleware-cannot-set-status-code-on-exception-because-response-ha
if (!context.Response.HasStarted)
{
context.Response.StatusCode = cacheResponseBody.StatusCode;
context.Response.ContentType = cacheResponseBody.ContentType;
await context.Response.WriteAsync(cacheResponseBody.Body);
_iLogger.LogInformation($"触发PostResponseCacheMiddleware本地缓存");
//直接return可避免此错误 :OnStarting cannot be set because the response has already started.
await Task.CompletedTask;
return;
}
else
{
_iLogger.LogError($"StatusCode无法设置,因为响应已经启动,位置为:触发本地缓存开始赋值[responsecache2]");
await Task.CompletedTask;
return;
}
}
else if (!context.RequestAborted.IsCancellationRequested)
{
Stream originalBody = context.Response.Body;
try
{
using (var memStream = new MemoryStream())
{
context.Response.Body = memStream;
await _next(context);
memStream.Position = 0;
string responseBody = new StreamReader(memStream).ReadToEnd();
responseCacheData = new ResponseCacheData
{
Body = responseBody,
ContentType = context.Response.ContentType,
StatusCode = context.Response.StatusCode
};
memStream.Position = 0;
await memStream.CopyToAsync(originalBody);
if (_responseCacheOption.Cluster)
{
_redisManager.Set($"NetProPostResponse:{requestStrKey}", new ResponseCacheData
{
Body = responseBody,
ContentType = context.Response.ContentType,
StatusCode = context.Response.StatusCode
}, TimeSpan.FromSeconds(_responseCacheOption.Duration));
}
else
{
_memorycache.Set<ResponseCacheData>($"NetProPostResponse:{requestStrKey}", new ResponseCacheData
{
Body = responseBody,
ContentType = context.Response.ContentType,
StatusCode = context.Response.StatusCode
}, TimeSpan.FromSeconds(_responseCacheOption.Duration));
}
}
await Task.CompletedTask;
return;
}
finally
{
context.Response.Body = originalBody;
}
}
else if (context.RequestAborted.IsCancellationRequested)
{
await Task.CompletedTask;
return;
}
}
else if (!context.RequestAborted.IsCancellationRequested)
{
goto gotoNext;
}
else
{
await Task.CompletedTask;
return;
}
}
gotoNext:
await _next(context);
}
//private async Task<string> ReadAsString(HttpContext context)
//{
// try
// {
// if (context.Request.ContentLength > 0)
// {
// EnableRewind(context.Request);
// var encoding = GetRequestEncoding(context.Request);
// return await ReadStream(context, encoding);
// }
// return null;
// }
// catch (Exception ex) when (!ex.Message?.Replace(" ", string.Empty).ToLower().Contains("unexpectedendofrequestcontent") ?? true)
// {
// _iLogger.LogError(ex, $"[ReadAsString] Post响应缓存读取body出错");
// return null;
// }
//}
//private async Task<string> ReadStream(HttpContext context, Encoding encoding)
//{
// using (StreamReader sr = new StreamReader(context.Request.Body, encoding, true, 1024, true))
// {
// if (context?.RequestAborted.IsCancellationRequested ?? true)
// return null;
// var str = await sr.ReadToEndAsync();
// context.Request.Body.Seek(0, SeekOrigin.Begin);
// return str;
// }
//}
//private Encoding GetRequestEncoding(HttpRequest request)
//{
// var requestContentType = request.ContentType;
// var requestMediaType = requestContentType == null ? default(MediaType) : new MediaType(requestContentType);
// var requestEncoding = requestMediaType.Encoding;
// if (requestEncoding == null)
// {
// requestEncoding = Encoding.UTF8;
// }
// return requestEncoding;
//}
//private void EnableRewind(HttpRequest request)
//{
// if (!request.Body.CanSeek)
// {
// request.EnableBuffering();
// }
// request.Body.Seek(0L, SeekOrigin.Begin);
//}
}
/// <summary>
///
/// </summary>
public static class PostResponseCacheMiddlewareExtensions
{
/// <summary>
/// 签名在响应缓存之前
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
/// <remarks></remarks>
public static IApplicationBuilder UsePostResponseCache(
this IApplicationBuilder builder)
{
var responseCacheOption = builder.ApplicationServices.GetService(typeof(ResponseCacheOption)) as ResponseCacheOption;
if (responseCacheOption?.Enabled ?? false)
{
if (responseCacheOption.Duration < 1)
throw new ArgumentNullException($"ResponseCacheOption.Duration", "Post响应缓存Duration参数不能小于1");
//脱离Http协议的Post缓存
builder.UseMiddleware<PostResponseCacheMiddleware>();
}
return builder;
}
/// <summary>
/// Get缓存
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
/// <remarks>默认Get全局缓存</remarks>
public static IApplicationBuilder UseGetResponseCaching(
this IApplicationBuilder builder)
{
var responseCacheOption = builder.ApplicationServices.GetService(typeof(ResponseCacheOption)) as ResponseCacheOption;
if (responseCacheOption?.Enabled ?? false)
{
//全局Get响应缓存,遵守Http协议
builder.UseResponseCaching();
builder.Use(async (context, next) =>
{
context.Response.GetTypedHeaders().CacheControl =
new CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromSeconds(responseCacheOption.Duration < 1 ? 1 : responseCacheOption.Duration)
};
var responseCachingFeature = context.Features.Get<IResponseCachingFeature>();
if (responseCachingFeature != null)//必须放于响应缓存之后
{
responseCachingFeature.VaryByQueryKeys = new[] { "*" };
}
await next();
});
}
return builder;
}
}
}