Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article explains how to configure Redis output caching middleware in an ASP.NET Core app. The output caching middleware enables caching of HTTP responses. The benefits of using output caching include:
- Saving web server resource utilization from repeatedly generating HTTP responses and rendering HTML webpages.
- Improving web request performance by reducing dependency calls.
The benefits of using Azure Cache for Redis as your output cache include:
- Saving web server memory resource utilization by storing cached content in an external Redis database.
- Improving web application resiliency by persisting cached content in an external Redis database in the scenario of server failover or restart.
You can use the output caching middleware in all types of ASP.NET Core apps: minimal API, web API with controllers, Model-View-Controller (MVC), and Razor Pages. For a detailed walkthrough of output caching syntax and features, see Output caching middleware in ASP.NET Core.
- Download and install .NET 8 SDK or later.
- Download and install Visual Studio Code.
- Create an Azure Cache for Redis instance. For more information, see:
For an end-to-end application that uses Redis output caching, see .NET 8 Web Application with Redis Output Caching and Azure OpenAI.
Here's the procedure for using Azure Cache for Redis as the output caching middleware in an ASP.NET Core app. The output caching middleware enables caching of HTTP responses as page output.
Open a command prompt. Use the .NET command line to create an empty ASP.NET core web application:
mkdir RedisOutputCache
cd RedisOutputCache
dotnet new web
Make sure your command prompt is in the project folder that contains the *.csproj
file. Install the Microsoft.AspNetCore.OutputCaching.StackExchangeRedis NuGet package:
dotnet add package Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
Use the command prompt to open the project directory in Visual Studio Code:
code .
If you're prompted, select Yes I trust the author to proceed.
Open the Program.cs file.
Add the
AddStackExchangeRedisOutputCache()
,AddOutputCache()
, andUseOutputCache()
function calls: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();
Save the Program.cs file. Make sure the application builds with no errors in the command prompt:
dotnet build
Add the cached endpoint under
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();
Save the Program.cs file. Make sure the application builds with no errors in the command prompt:
dotnet build
It's a security best practice to avoid storing passwords in clear text in source-controlled code files. ASP.NET Core provides user secrets management to help ensure that secrets, such as connection strings, are saved and accessed securely. Use this feature to manage the Azure Cache for Redis connection strings.
Enable the storage of secrets by using the Azure CLI:
dotnet user-secrets init
Obtain the Azure Cache for Redis connection strings by using the Azure portal.
You can find the connection string for open source Redis tiers by selecting Authentication on the Resource menu. Here's an example string:
<Azure_redis_name>.redis.cache.chinacloudapi.cn:6380,password=<Azure_redis_primary_accesskey>,ssl=True,abortConnect=False
.Set the Redis connection for the web application by using the Azure CLI:
dotnet user-secrets set RedisCacheConnection <Azure_Redis_ConnectionString>
Run your application:
dotnet build dotnet run
The command prompt displays progress:
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. ...
Browse to the local host URL. Here's an example:
http://localhost:5063/cached
.Observe if the current time is being displayed. Refreshing the browser doesn't change the time because the content is cached. The
/cached
endpoint might display text like the following example:Hello Redis Output Cache5/27/2024 8:31:35 PM