本文介绍如何在 ASP.NET Core 应用中配置 Redis 输出缓存中间件。 输出缓存中间件支持 HTTP 响应的缓存。 使用输出缓存的好处包括:
- 通过反复生成 HTTP 响应和呈现 HTML 网页来节省 Web 服务器资源利用率。
- 通过减少依赖项调用来提高 Web 请求性能。
将 Azure Cache for Redis 用作输出缓存的好处包括:
- 通过将缓存的内容存储在外部 Redis 数据库中来节省 Web 服务器内存资源利用率。
- 在服务器故障转移或重启的情况下,通过在外部 Redis 数据库中保留缓存的内容来提高 Web 应用程序复原能力。
可以在所有类型的 ASP.NET Core 应用中使用输出缓存中间件:最小 API、具有控制器的 Web API、模型-视图-控制器 (MVC) 和 Razor Pages。 有关输出缓存语法和功能的详细演练,请参阅 ASP.NET Core 中的输出缓存中间件。
先决条件
- 下载并安装 .NET 8 SDK 或更高版本。
- 下载并安装 Visual Studio Code。
- 创建 Azure Cache for Redis 实例。 有关详细信息,请参阅:
有关使用 Redis 输出缓存的端到端应用程序,请参阅采用 Redis 输出缓存和 Azure OpenAI 的 .NET 8 Web 应用程序。
将 Redis 输出缓存中间件添加到空的 ASP.NET Core Web 应用程序
下面是在 ASP.NET Core 应用中将 Azure Cache for Redis 用作输出缓存中间件的过程。 输出缓存中间件支持将 HTTP 响应缓存为页面输出。
创建空的 ASP.NET Core Web 应用程序
打开命令提示符。 使用 .NET 命令行创建空的 ASP.NET Core Web 应用程序:
mkdir RedisOutputCache
cd RedisOutputCache
dotnet new web
为 Redis 输出缓存添加 NuGet 包
请确保命令提示符位于包含 *.csproj
文件的项目文件夹中。 安装 Microsoft.AspNetCore.OutputCaching.StackExchangeRedis NuGet 包:
dotnet add package Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
在 Web 应用程序的启动代码中添加 Redis 输出缓存中间件
使用命令提示符在 Visual Studio Code 中打开项目目录:
code .
如果系统提示,请选择“是,我相信作者”以继续操作。
打开 Program.cs 文件。
添加
AddStackExchangeRedisOutputCache()
、AddOutputCache()
和UseOutputCache()
函数调用:var builder = WebApplication.CreateBuilder(args); // add Redis Output Cache Middleware service builder.Services.AddStackExchangeRedisOutputCache(options => { options.Configuration = builder.Configuration["RedisCacheConnection"]; }); builder.Services.AddOutputCache(options => { // optional: named output-cache profiles }); var app = builder.Build(); app.MapGet("/", () => "Hello World!"); // use Redis Output Caching Middleware service app.UseOutputCache(); app.Run();
保存 Program.cs 文件。 请确保应用程序生成时在命令提示符中没有错误:
dotnet build
配置一个终结点或页面
在
app.MetGet()
下添加缓存的终结点:app.MapGet("/", () => "Hello World!"); //Added for caching HTTP response of one end point app.MapGet("/cached",()=> "Hello Redis Output Cache" + DateTime.Now).CacheOutput();
保存 Program.cs 文件。 请确保应用程序生成时在命令提示符中没有错误:
dotnet build
配置 Redis 缓存连接
安全的最佳做法是避免在由源代码控制的代码文件中以明文形式存储密码。 ASP.NET Core 提供用户机密管理来帮助确保安全地保存和访问机密(如连接字符串)。 使用此功能可管理 Azure Cache for Redis 连接字符串。
使用 Azure CLI 启用机密存储:
dotnet user-secrets init
使用 Azure 门户获取 Azure Cache for Redis 连接字符串。
可以通过在“资源”菜单中选择“身份验证”来查找开源 Redis 层的连接字符串。 下面是一个示例字符串:
<Azure_redis_name>.redis.cache.chinacloudapi.cn:6380,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False
。可以通过在“资源”菜单上选择“访问密钥”来查找 Azure 托管 Redis 的访问密钥(预览版)。 连接字符串可以通过“资源”菜单的“概述”部分中的其他 Redis 信息获取。 下面是一个示例字符串:
<Azure_redis_name>.<Azure_region>.redisenterprise.cache.chinacloudapi.cn:10000,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False
。使用 Azure CLI 为 Web 应用程序设置 Redis 连接:
dotnet user-secrets set RedisCacheConnection <Azure_Redis_ConnectionString>
运行应用程序:
dotnet build dotnet run
命令提示符会显示进度:
Building... info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5063 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. ...
验证 Redis 输出缓存是否正常工作
浏览到本地主机 URL。 下面是一个示例:
http://localhost:5063/cached
。观察当前时间是否被显示出来。 刷新浏览器时,不会更改时间,因为内容已缓存。
/cached
终结点可能会显示如下例所示的文本:Hello Redis Output Cache5/27/2024 8:31:35 PM