.NET Aspire StackExchange Redis output caching component

In this article, you learn how to use the .NET Aspire StackExchange Redis output caching component. The Aspire.StackExchange.Redis.OutputCaching library is used to register an ASP.NET Core Output Caching provider backed by a Redis server. It enables corresponding health check, logging, and telemetry.

Get started

To get started with the .NET Aspire StackExchange Redis output caching component, install the Aspire.StackExchange.Redis.OutputCaching NuGet package.

dotnet add package Aspire.StackExchange.Redis.OutputCaching

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the AddRedisOutputCache extension to register the required services for output caching.

builder.AddRedisOutputCache();

Add the middleware to the request processing pipeline by calling UseOutputCache.

var app = builder.Build();
app.UseOutputCache();

For minimal API apps, configure an endpoint to do caching by calling CacheOutput, or by applying the OutputCacheAttribute, as shown in the following examples:

app.MapGet("/cached", () => { return "Hello world!"; }).CacheOutput();
app.MapGet("/attribute", [OutputCache] () => { return "Hello world!"; });

For apps with controllers, apply the [OutputCache] attribute to the action method. For Razor Pages apps, apply the attribute to the Razor page class.

App host usage

In your app host project, register the .NET Aspire StackExchange Redis output caching component using the AddRedis method and consume the service using the following methods:

// Service registration
var redis = builder.AddRedis("redis");

// Service consumption
var basket = builder.AddProject<Projects.ExampleProject>()
                    .WithReference(redis)

The WithReference method configures a connection in the ExampleProject project named redis. In the Program.cs file of ExampleProject, the Redis connection can be consumed using:

builder.AddRedisOutputCache("messaging");

Configuration

The .NET Aspire StackExchange Redis output caching component provides multiple options to configure the Redis connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddRedisOutputCache:

builder.AddRedisOutputCache("RedisConnection");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "RedisConnection": "localhost:6379"
  }
}

For more information on how to format this connection string, see the StackExchange Redis configuration docs.

Use configuration providers

The .NET Aspire StackExchange Redis output caching component supports Microsoft.Extensions.Configuration. It loads the StackExchangeRedisSettings from configuration by using the Aspire:StackExchange:Redis key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "StackExchange": {
      "Redis": {
        "ConfigurationOptions": {
          "ConnectTimeout": 3000,
          "ConnectRetry": 2
        },
        "HealthChecks": false,
        "Tracing": true
      }
    }
  }
}

Use inline delegates

You can also pass the Action<StackExchangeRedisSettings> configurationSettings delegate to set up some or all the options inline, for example to disable health checks from code:

builder.AddRedisOutputCache(
    "cache",
    static settings => settings.HealthChecks = false);

You can also set up the ConfigurationOptions using the Action<ConfigurationOptions> configureOptions delegate parameter of the AddRedisOutputCache method. For example to set the connection timeout:

builder.AddRedisOutputCache(
    "cache",
    static configureOptions: options => options.ConnectTimeout = 3000);

Health checks

By default, .NET Aspire components enable health checks for all services. For more information, see .NET Aspire components overview.

The .NET Aspire StackExchange Redis output caching component handles the following:

  • Adds the StackExchange.Redis health check, tries to open the connection and throws when it fails.
  • Integrates with the /health HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.

Observability and telemetry

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire StackExchange Redis output caching component uses the following Log categories:

  • Aspire.StackExchange.Redis
  • Microsoft.AspNetCore.OutputCaching.StackExchangeRedis

Tracing

The .NET Aspire StackExchange Redis output caching component will emit the following Tracing activities using OpenTelemetry:

  • "OpenTelemetry.Instrumentation.StackExchangeRedis"

Metrics

The .NET Aspire StackExchange Redis output caching component currently doesn't support metrics by default due to limitations with the StackExchange.Redis library.

See also