Aller au contenu

Azure Cache for Redis Client integration

Ce contenu n’est pas encore disponible dans votre langue.

The Aspire Azure Cache for Redis client integration is used to connect to an Azure Redis cache using StackExchange.Redis. To get started with the Aspire Azure Cache for Redis client integration, install the 📦 Aspire.StackExchange.Redis NuGet package.

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

In the Program.cs file of your client-consuming project, call the AddRedisClient extension method to register an IConnectionMultiplexer for use via the dependency injection container. The method takes a connection name parameter:

builder.AddRedisClient(connectionName: "redis");

After adding the IConnectionMultiplexer, you can retrieve the connection instance using dependency injection:

public class ExampleService(IConnectionMultiplexer redis)
{
// Use redis...
}

For more information, see:

Properties of the Azure Cache for Redis resources

Section titled “Properties of the Azure Cache for Redis resources”

When you use the WithReference method to pass an Azure Cache for Redis resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Host property of a resource called redis becomes REDIS_HOST.

The Azure Managed Redis resource exposes the following connection properties:

Property NameDescription
HostThe Redis server hostname
PortThe Redis server port (default: 10000)
UriThe connection URI with TLS (rediss://)

When access key authentication is enabled:

Property NameDescription
PasswordThe access key from Key Vault

For example, if you reference an Azure Managed Redis resource named redis in your AppHost project, the following environment variables will be available in the consuming project:

  • REDIS_HOST
  • REDIS_PORT
  • REDIS_URI

For caching scenarios, you can add a Redis-based distributed cache using the AddRedisDistributedCache method:

builder.AddRedisDistributedCache(connectionName: "redis");

This registers an IDistributedCache implementation backed by Redis:

public class ExampleService(IDistributedCache cache)
{
// Use cache...
}

For output caching scenarios in ASP.NET Core, you can add a Redis-based output cache using the AddRedisOutputCache method:

builder.AddRedisOutputCache(connectionName: "redis");

This configures Redis as the output cache store for your web application.

There might be situations where you want to register multiple IConnectionMultiplexer instances with different connection names. To register keyed Redis clients, call the AddKeyedRedisClient method:

builder.AddKeyedRedisClient(name: "primary-cache");
builder.AddKeyedRedisClient(name: "secondary-cache");

Then you can retrieve the connection instances using dependency injection:

public class ExampleService(
[KeyedService("primary-cache")] IConnectionMultiplexer primaryRedis,
[KeyedService("secondary-cache")] IConnectionMultiplexer secondaryRedis)
{
// Use redis connections...
}

For more information, see Keyed services in .NET.

The Aspire Azure Cache for Redis library provides multiple options to configure the Redis connection based on the requirements and conventions of your project. A ConnectionString is required.

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

builder.AddRedisClient(connectionName: "redis");

The connection information is retrieved from the ConnectionStrings configuration section:

{
"ConnectionStrings": {
"redis": "myredis.redis.cache.windows.net:6380,password=your_password,ssl=True"
}
}

The library supports Microsoft.Extensions.Configuration. It loads settings from configuration using the Aspire:StackExchange:Redis key:

{
"Aspire": {
"StackExchange": {
"Redis": {
"DisableHealthChecks": true,
"DisableTracing": false
}
}
}
}

You can configure settings inline:

builder.AddRedisClient(
"redis",
settings => settings.DisableHealthChecks = true);

Aspire integrations automatically set up Logging, Tracing, and Metrics configurations.

The Aspire Azure Cache for Redis integration uses standard .NET logging for connection events and errors.

The Aspire Azure Cache for Redis integration will emit the following tracing activities using OpenTelemetry:

  • StackExchange.Redis.*

The Aspire Azure Cache for Redis integration currently doesn’t expose custom metrics by default, but uses standard Redis monitoring capabilities.