Valkey integration
Ce contenu n’est pas encore disponible dans votre langue.
Valkey is a Redis fork and complies with the Redis serialization protocol (RESP). It’s a high-performance key/value datastore that supports a variety of workloads such as caching, message queues, and can act as a primary database. The Valkey integration enables you to connect to existing Valkey instances, or create new instances from Aspire with the docker.io/valkey/valkey container image.
Hosting integration
Section titled “Hosting integration”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 add valkeyLa CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :
Select an integration to add:
> valkey (Aspire.Hosting.Valkey)> Other results listed as selectable options...#:package Aspire.Hosting.Valkey@*<PackageReference Include="Aspire.Hosting.Valkey" Version="*" />Add Valkey resource
Section titled “Add Valkey resource”In your AppHost project, call AddValkey on the builder instance to add a Valkey resource:
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.
Add Valkey resource with data volume
Section titled “Add Valkey resource with data volume”To add a data volume to the Valkey resource, call the WithDataVolume method:
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.
Add Valkey resource with data bind mount
Section titled “Add Valkey resource with data bind mount”To add a data bind mount to the Valkey resource, call the WithDataBindMount method:
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);Add Valkey resource with persistence
Section titled “Add Valkey resource with persistence”To add persistence to the Valkey resource, call the WithPersistence method with either the data volume or data bind mount:
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.
Connection properties
Section titled “Connection properties”When you reference a Valkey resource using WithReference, the following connection properties are made available to the consuming project:
Valkey
Section titled “Valkey”The Valkey resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the Valkey server |
Port | The port number the Valkey server is listening on |
Password | The password for authentication |
Uri | The connection URI, with the format valkey://:{Password}@{Host}:{Port} |
Example connection string:
Uri: valkey://:p%40ssw0rd1@localhost:6379Hosting integration health checks
Section titled “Hosting integration health checks”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.
Client integration
Section titled “Client integration”Valkey is Redis-compatible, so you use the same Redis client packages. To get started, install the 📦 Aspire.StackExchange.Redis NuGet package:
dotnet add package Aspire.StackExchange.Redis#:package Aspire.StackExchange.Redis@*<PackageReference Include="Aspire.StackExchange.Redis" Version="*" />Add Valkey client
Section titled “Add Valkey client”In the Program.cs file of your client-consuming project, call the AddRedisClient extension method to register an IConnectionMultiplexer:
builder.AddRedisClient(connectionName: "cache");You can then retrieve the IConnectionMultiplexer instance using dependency injection:
public class ExampleService(IConnectionMultiplexer connectionMux){ // Use connection multiplexer...}Configuration and other client features
Section titled “Configuration and other client features”Since Valkey is Redis-compatible, all Redis client configuration, keyed services, health checks, and observability features work the same way. See the Redis integration page for complete client integration documentation.
Distributed caching and output caching
Section titled “Distributed caching and output caching”Valkey also supports Redis distributed caching and output caching. Install the respective packages:
- For distributed caching: 📦 Aspire.StackExchange.Redis.DistributedCaching
- For output caching: 📦 Aspire.StackExchange.Redis.OutputCaching
See Redis Distributed Caching and Redis Output Caching for usage details.