Zum Inhalt springen
Docs Try Aspire
Docs Try

Redis hosting integration

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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 Paket hinzufügen
aspire add redis

Die Aspire CLI ist interaktiv; das passende Suchergebnis wählen, wenn gefragt:

Aspire CLI — Beispielausgabe
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.

Pass connection information to app resources

Section titled “Pass connection information to app resources”

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 an app resource 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);
});