title | description | ms.topic | ms.date |
---|---|---|---|
.NET Aspire StackExchange Redis output caching Component |
This article describes the .NET Aspire StackExchange Redis output caching component features and capabilities |
how-to |
04/29/2024 |
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.
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
<PackageReference Include="Aspire.StackExchange.Redis.OutputCache"
Version="[SelectVersion]" />
For more information, see dotnet add package or Manage package dependencies in .NET applications.
In the :::no-loc text="Program.cs"::: file of your component-consuming project, call the xref:Microsoft.Extensions.Hosting.AspireRedisOutputCacheExtensions.AddRedisOutputCache%2A 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 xref:Microsoft.Extensions.DependencyInjection.OutputCacheConventionBuilderExtensions.CacheOutput%2A, or by applying the xref:Microsoft.AspNetCore.OutputCaching.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.
[!INCLUDE redis-app-host]
// Service registration
var redis = builder.AddRedis("redis");
// Service consumption
var basket = builder.AddProject<Projects.ExampleProject>()
.WithReference(redis)
The xref:Aspire.Hosting.ResourceBuilderExtensions.WithReference%2A method configures a connection in the ExampleProject
project named redis
. In the :::no-loc text="Program.cs"::: file of ExampleProject
, the Redis connection can be consumed using:
builder.AddRedisOutputCache("messaging");
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.
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.
The .NET Aspire StackExchange Redis output caching component supports xref:Microsoft.Extensions.Configuration. It loads the xref:Aspire.StackExchange.Redis.StackExchangeRedisSettings from configuration by using the Aspire:StackExchange:Redis
key. Example :::no-loc text="appsettings.json"::: that configures some of the options:
{
"Aspire": {
"StackExchange": {
"Redis": {
"ConfigurationOptions": {
"ConnectTimeout": 3000,
"ConnectRetry": 2
},
"DisableHealthChecks": true,
"DisableTracing": false
}
}
}
}
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.DisableHealthChecks = true);
You can also set up the ConfigurationOptions using the Action<ConfigurationOptions> configureOptions
delegate parameter of the xref:Microsoft.Extensions.Hosting.AspireRedisOutputCacheExtensions.AddRedisOutputCache%2A method. For example to set the connection timeout:
builder.AddRedisOutputCache(
"cache",
static configureOptions: options => options.ConnectTimeout = 3000);
[!INCLUDE component-health-checks]
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.
[!INCLUDE component-observability-and-telemetry]
The .NET Aspire StackExchange Redis output caching component uses the following Log categories:
Aspire.StackExchange.Redis
Microsoft.AspNetCore.OutputCaching.StackExchangeRedis
The .NET Aspire StackExchange Redis output caching component will emit the following Tracing activities using OpenTelemetry:
- "OpenTelemetry.Instrumentation.StackExchangeRedis"
The .NET Aspire StackExchange Redis output caching component currently doesn't support metrics by default due to limitations with the StackExchange.Redis
library.