Azure Cache for Redis Client integration
이 콘텐츠는 아직 번역되지 않았습니다.
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.
dotnet add package Aspire.StackExchange.Redis#:package Aspire.StackExchange.Redis@*<PackageReference Include="Aspire.StackExchange.Redis" Version="*" />Add Redis client
Section titled “Add Redis client”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:
- StackExchange.Redis documentation for examples on using the
IConnectionMultiplexer. - Dependency injection in .NET for details on dependency injection.
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.
Azure Managed Redis
Section titled “Azure Managed Redis”The Azure Managed Redis resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The Redis server hostname |
Port | The Redis server port (default: 10000) |
Uri | The connection URI with TLS (rediss://) |
When access key authentication is enabled:
| Property Name | Description |
|---|---|
Password | The 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_HOSTREDIS_PORTREDIS_URI
Add Redis distributed cache
Section titled “Add Redis distributed cache”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...}Add Redis output cache
Section titled “Add Redis output 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.
Add keyed Redis client
Section titled “Add keyed Redis client”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.
Configuration
Section titled “Configuration”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.
Use a connection string
Section titled “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 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" }}Use configuration providers
Section titled “Use configuration providers”The library supports Microsoft.Extensions.Configuration. It loads settings from configuration using the Aspire:StackExchange:Redis key:
{ "Aspire": { "StackExchange": { "Redis": { "DisableHealthChecks": true, "DisableTracing": false } } }}Use inline delegates
Section titled “Use inline delegates”You can configure settings inline:
builder.AddRedisClient( "redis", settings => settings.DisableHealthChecks = true);Observability and telemetry
Section titled “Observability and telemetry”Aspire integrations automatically set up Logging, Tracing, and Metrics configurations.
Logging
Section titled “Logging”The Aspire Azure Cache for Redis integration uses standard .NET logging for connection events and errors.
Tracing
Section titled “Tracing”The Aspire Azure Cache for Redis integration will emit the following tracing activities using OpenTelemetry:
StackExchange.Redis.*
Metrics
Section titled “Metrics”The Aspire Azure Cache for Redis integration currently doesn’t expose custom metrics by default, but uses standard Redis monitoring capabilities.