Перейти до вмісту

Valkey hosting integration

Цей контент ще не доступний вашою мовою.

Valkey logo

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

Aspire CLI — Додати пакет Aspire.Hosting.Valkey
aspire add valkey

Aspire CLI інтерактивний; оберіть відповідний результат пошуку:

Aspire CLI — Приклад виводу
Select an integration to add:
> valkey (Aspire.Hosting.Valkey)
> Other results listed as selectable options...

In your AppHost project, call AddValkey on the builder instance to add a Valkey resource:

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

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

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

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

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

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

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

To add persistence to the Valkey 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.AddValkey("cache")
.WithDataVolume()
.WithPersistence(
interval: TimeSpan.FromMinutes(5),
keysChangedThreshold: 100);
var myService = builder.AddProject<Projects.ExampleProject>()
.WithReference(cache);

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

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