Skip to content

Latest commit

 

History

History
114 lines (74 loc) · 4.79 KB

azure-cache-for-redis-output-caching-integration.md

File metadata and controls

114 lines (74 loc) · 4.79 KB
title description ms.date
Azure Cache for Redis output caching integration
Learn how to integrate Azure Cache for Redis as an output caching solution with the .NET Aspire stack.
02/05/2025

.NET Aspire Azure Cache for Redis®* output caching integration

[!INCLUDE includes-hosting-and-client]

[!INCLUDE azure-redis-intro]

The .NET Aspire Azure Cache for Redis integration enables you to connect to existing Azure Cache for Redis instances, or create new instances from .NET with the docker.io/library/redis container image.

Hosting integration

[!INCLUDE azure-redis-app-host]

Client integration

[!INCLUDE redis-output-client-nuget]

Add output caching

In the :::no-loc text="Program.cs"::: file of your client-consuming project, call the xref:Microsoft.Extensions.Hosting.AspireRedisOutputCacheExtensions.AddRedisOutputCache%2A extension method on any xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder to register the required services for output caching.

builder.AddRedisOutputCache(connectionName: "cache");

Tip

The connectionName parameter must match the name used when adding the Azure Cache for Redis resource in the app host project. For more information, see Add Azure Cache for Redis resource.

Add the middleware to the request processing pipeline by calling xref:Microsoft.AspNetCore.Builder.OutputCacheApplicationBuilderExtensions.UseOutputCache(Microsoft.AspNetCore.Builder.IApplicationBuilder):

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", () => "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.

[!INCLUDE azure-redis-output-client]

Configuration

The .NET Aspire Stack Exchange Redis output caching integration 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 xref:Microsoft.Extensions.Hosting.AspireRedisOutputCacheExtensions.AddRedisOutputCache*:

builder.AddRedisOutputCache(connectionName: "cache");

Then the connection string will be retrieved from the ConnectionStrings configuration section:

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

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

Use configuration providers

[!INCLUDE redis-output-client-json-settings]

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.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 settings => settings.ConnectTimeout = 3_000);

[!INCLUDE redis-output-client-health-checks-and-diagnostics]

See also

[!INCLUDE redis-trademark]