Redis Output Caching client integration
To get started with the Redis output caching integration, install the 📦 Aspire.StackExchange.Redis.OutputCaching NuGet package:
dotnet add package Aspire.StackExchange.Redis.OutputCachingAdd output caching
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");
Tip
The connectionName parameter must match the name used when adding the Redis
resource in the AppHost project.
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.
Configuration
Use a connection string
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"
}
}
Use configuration providers
The Redis output caching integration supports Microsoft.Extensions.Configuration. Example appsettings.json:
{
"Aspire": {
"StackExchange": {
"Redis": {
"OutputCaching": {
"ConnectionString": "localhost:6379",
"DisableHealthChecks": false,
"DisableTracing": false
}
}
}
}
}
Use inline delegates
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",
configureOptions: options => options.ConnectTimeout = 3000);
Client integration health checks
By default, Aspire integrations enable health checks. The Redis output caching integration adds a health check that verifies the Redis instance is reachable.
Observability and telemetry
Logging
The Redis output caching integration uses standard .NET logging.
Tracing
The integration emits tracing activities using OpenTelemetry.
Metrics
The integration emits metrics using OpenTelemetry.
[!TIP] Registered trademark Redis is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Any use by Microsoft is for referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and Microsoft.