Gå til indhold

Valkey integration

Dette indhold er ikke tilgængeligt i dit sprog endnu.

Valkey logo

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.

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 — Tilføj Aspire.Hosting.Valkey-pakke
aspire add valkey

Aspire CLI er interaktiv; vælg det passende søgeresultat når du bliver spurgt:

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

When you reference a Valkey resource using WithReference, the following connection properties are made available to the consuming project:

The Valkey resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Valkey server
PortThe port number the Valkey server is listening on
PasswordThe password for authentication
UriThe connection URI, with the format valkey://:{Password}@{Host}:{Port}

Example connection string:

Uri: valkey://:p%40ssw0rd1@localhost:6379

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.

Valkey is Redis-compatible, so you use the same Redis client packages. To get started, install the 📦 Aspire.StackExchange.Redis NuGet package:

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

builder.AddRedisClient(connectionName: "cache");

You can then retrieve the IConnectionMultiplexer instance using dependency injection:

public class ExampleService(IConnectionMultiplexer connectionMux)
{
// Use connection multiplexer...
}

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.

Valkey also supports Redis distributed caching and output caching. Install the respective packages:

See Redis Distributed Caching and Redis Output Caching for usage details.

Spørg & svarSamarbejdFællesskabDiskutérSe