Skip to content

Redis Output Caching

Redis logo

The Redis® output caching integration is used to register an ASP.NET Core Output Caching provider backed by a Redis server with the docker.io/library/redis container image.

The Redis hosting integration models a Redis resource as the RedisResource type. To access this type and APIs, add the 📦 Aspire.Hosting.Redis NuGet package in your AppHost project:

Aspire CLI — Add Aspire.Hosting.Redis package
aspire add redis

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> redis (Aspire.Hosting.Redis)
> Other results listed as selectable options...

In your AppHost project, call AddRedis on the builder instance to add a Redis resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

When Aspire adds a container image to the AppHost, it creates a new Redis instance on your local machine.

To add a data volume to the Redis resource, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithDataVolume(isReadOnly: false);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

The data volume is used to persist the Redis data outside the lifecycle of its container. The data volume is mounted at the /data path in the Redis container.

To add persistence to the Redis resource, call the WithPersistence method with either the data volume or data bind mount:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithDataVolume()
.WithPersistence(
interval: TimeSpan.FromMinutes(5),
keysChangedThreshold: 100);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

The preceding code adds persistence to the Redis resource by taking snapshots of the Redis data at a specified interval and threshold.

For more Redis hosting integration options, see the main Redis integration page.

To get started with the Redis output caching integration, install the 📦 Aspire.StackExchange.Redis.OutputCaching NuGet package:

.NET CLI — Add Aspire.StackExchange.Redis.OutputCaching package
dotnet add package Aspire.StackExchange.Redis.OutputCaching

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

builder.AddRedisOutputCache(connectionName: "cache");

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:

app.MapGet("/cached", () => "Hello world!")
.CacheOutput();
app.MapGet(
"/attribute",
[OutputCache] () => "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.

When using a connection string from the ConnectionStrings configuration section:

builder.AddRedisOutputCache(connectionName: "cache");

Then the connection string will be retrieved:

{
"ConnectionStrings": {
"cache": "localhost:6379"
}
}

The Redis output caching integration supports Microsoft.Extensions.Configuration. Example appsettings.json:

{
"Aspire": {
"StackExchange": {
"Redis": {
"OutputCaching": {
"ConnectionString": "localhost:6379",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}
}

You can pass the delegate to set up options inline:

builder.AddRedisOutputCache(
"cache",
static settings => settings.DisableHealthChecks = true);

You can also configure the ConfigurationOptions:

builder.AddRedisOutputCache(
"cache",
static settings => settings.ConnectTimeout = 3_000);

By default, Aspire integrations enable health checks. The Redis output caching integration adds a health check that verifies the Redis instance is reachable.

The Redis output caching integration uses standard .NET logging.

The integration emits tracing activities using OpenTelemetry.

The integration emits metrics using OpenTelemetry.

FAQCollaborateCommunityDiscussWatch