asp.net-core - 如何在 Asp.Net Core 中间件中缓存响应?
问题描述
我在 ASP.NET Core 3.1 中创建了一个中间件,以从 route 返回 XML Sitemap /sitemap.xml
。
public class SitemapMiddleware {
private readonly RequestDelegate _next;
public SitemapMiddleware(RequestDelegate next) {
_next = next;
}
public async Task InvokeAsync(HttpContext context, ISitemapService sitemapService) {
if (context.Request.Path.StartsWithSegments("/sitemap.xml)) {
Sitemap sitemap = sitemapService.GetSitemap();
context.Response.ContentType = "application/xml";
await context.Response.WriteAsync(sitemap.ToString(), Encoding.UTF8);
} else {
await _next(context);
}
}
我将其注册如下:
public static class SitemapMiddlewareExtensions {
public static IApplicationBuilder UseSitemap(this IApplicationBuilder builder) {
return builder.MapWhen(x => x.Request.Path.StartsWithSegments("/sitemap.xml"),
x => x.UseMiddleware<SitemapMiddleware>(route));
}
}
问题
如果缓存的响应超过 24 小时,如何缓存中间件的响应并调用 SitemapService?
解决方案
推荐阅读
- python - “K-means”聚类分析
- java - 无法在 Spring Boot 中访问 WSDL 端点
- javascript - MaterialTable 动态查找
- amazon-dynamodb - 从表 dynamo DB 中获取所有数据
- algorithm - KD Tree: meaning of `leafsize` parameter
- javascript - 点击未定义(页面加载时取消静音流)
- firebase - Flutter - 无法在初始化程序中访问实例成员“remoteConfig”
- sql-server - 根据输入对象列表过滤 DACPAC 文件
- bash - 使用生成的字符串作为命令的 sed
- javascript - 电子远程(关闭、最小化等)不起作用(无法读取未定义的属性“getCurrentWindow”)