콘텐츠로 이동

Get started with the Azure Cache for Redis integrations

이 콘텐츠는 아직 번역되지 않았습니다.

Azure Cache for Redis logo

Azure Cache for Redis provides an in-memory data store based on the Redis software. Redis improves the performance and scalability of an application that uses backend data stores heavily. It’s able to process large volumes of application requests by keeping frequently accessed data in the server memory, which can be written to and read from quickly.

In this introduction, you’ll see how to install and use the Aspire Azure Cache for Redis integrations in a simple configuration. If you already have this knowledge, see Azure Cache for Redis Hosting integration for full reference details.

To begin, install the Aspire Azure Cache for Redis Hosting integration in your Aspire AppHost project:

Aspire CLI — Aspire.Hosting.Azure.Redis 패키지 추가
aspire add azure-redis

Aspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:

Aspire CLI — 출력 예시
Select an integration to add:
> azure-redis (Aspire.Hosting.Azure.Redis)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure Cache for Redis resource and pass it to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddAzureManagedRedis("redis");
builder.AddProject<Projects.ExampleProject>()
.WithReference(redis);
// After adding all resources, run the app...
builder.Build().Run();

To get started with the Aspire Azure Cache for Redis client integration, install the 📦 Aspire.StackExchange.Redis NuGet package in the client-consuming project:

.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:

C# — Program.cs
builder.AddRedisClient(connectionName: "redis");

Use injected Azure Cache for Redis properties

Section titled “Use injected Azure Cache for Redis properties”

In the AppHost, when you used the WithReference method to pass an Azure Cache for Redis resource to a consuming client project, Aspire injects several configuration properties that you can 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.

Use the GetValue() method to obtain these environment variables in consuming projects:

C# — Obtain configuration properties
string redisHost = builder.Configuration.GetValue<string>("REDIS_HOST");
string redisPort = builder.Configuration.GetValue<string>("REDIS_PORT");
string redisUri = builder.Configuration.GetValue<string>("REDIS_URI");

Use Azure Cache for Redis resources in client code

Section titled “Use Azure Cache for Redis resources in client code”

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

C# — ExampleService.cs
public class ExampleService(IConnectionMultiplexer redis)
{
// Use redis...
}

For more information, see StackExchange.Redis documentation for examples on using the IConnectionMultiplexer.