콘텐츠로 이동

Redis hosting integration

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

The Redis hosting integration models a Redis resource as the RedisResource type. To access this type and APIs, add the 📦 Aspire.Hosting.Redis NuGet package in your AppHost project:

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

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

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

In your AppHost project, call AddRedis on the builder instance to add a Redis resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

When Aspire adds a container image to the AppHost, it creates a new Redis instance on your local machine.

To add Redis Insights to the Redis resource, call the WithRedisInsight method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithRedisInsight();
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

Redis Insights is a free graphical interface for analyzing Redis data across all operating systems and Redis deployments.

To add Redis Commander to the Redis resource, call the WithRedisCommander method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithRedisCommander();
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

Redis Commander is a Node.js web application used to view, edit, and manage a Redis Database.

To add a data volume to the Redis resource, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithDataVolume(isReadOnly: false);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

The data volume is used to persist the Redis data outside the lifecycle of its container. The data volume is mounted at the /data path in the Redis container.

To add a data bind mount to the Redis resource, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithDataBindMount(
source: @"C:\Redis\Data",
isReadOnly: false);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

To add persistence to the Redis resource, call the WithPersistence method with either the data volume or data bind mount:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithDataVolume()
.WithPersistence(
interval: TimeSpan.FromMinutes(5),
keysChangedThreshold: 100);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

The preceding code adds persistence to the Redis resource by taking snapshots of the Redis data at a specified interval and threshold.

The Redis hosting integration automatically adds a health check for the Redis resource. The health check verifies that the Redis instance is running and that a connection can be established to it.

The Redis hosting integration can be used with any application technology. When you use WithReference to reference a Redis resource, connection information is automatically injected as environment variables:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddRedis("cache")
.WithLifetime(ContainerLifetime.Persistent);
// Configure a non-.NET application with Redis access
var app = builder.AddExecutable("my-app", "python", "app.py", ".")
.WithReference(redis) // Provides ConnectionStrings__cache
.WithEnvironment(context =>
{
context.EnvironmentVariables["REDIS_HOST"] = redis.Resource.PrimaryEndpoint.Property(EndpointProperty.Host);
context.EnvironmentVariables["REDIS_PORT"] = redis.Resource.PrimaryEndpoint.Property(EndpointProperty.Port);
});