ASP.NET Core Output Cache Provider for Azure Cache for Redis

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.

Prerequisites

For an end-to-end application that uses Redis output caching, see .NET 8 Web Application with Redis Output Caching and Azure OpenAI.

Add Redis output caching middleware to an empty ASP.NET core web application

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.

Create an empty ASP.NET core web application

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

Add NuGet packages for Redis output caching

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

Add Redis output caching middleware in the web application's startup code

  1. 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.

  2. Open the Program.cs file.

  3. Add the AddStackExchangeRedisOutputCache(), AddOutputCache(), and UseOutputCache() 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();
    
    
  4. Save the Program.cs file. Make sure the application builds with no errors in the command prompt:

    dotnet build
    

Configure one endpoint or page

  1. 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();
    
  2. Save the Program.cs file. Make sure the application builds with no errors in the command prompt:

    dotnet build
    

Configure the Redis Cache connection

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.

  1. Enable the storage of secrets by using the Azure CLI:

    dotnet user-secrets init
    
  2. 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.

  3. Set the Redis connection for the web application by using the Azure CLI:

    dotnet user-secrets set RedisCacheConnection <Azure_Redis_ConnectionString>
    
  4. 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.
    ...
    

Verify that the Redis output cache is working

  1. Browse to the local host URL. Here's an example: http://localhost:5063/cached.

  2. 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