コンテンツにスキップ

Garnet integration

このコンテンツはまだ日本語訳がありません。

Garnet logo

Garnet is a high-performance cache-store from Microsoft Research that complies with the Redis serialization protocol (RESP). The Garnet integration enables you to connect to existing Garnet instances, or create new instances from Aspire with the ghcr.io/microsoft/garnet container image.

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

Aspire CLI — Aspire.Hosting.Garnet パッケージを追加
aspire add garnet

Aspire CLI は対話的です。求められたら適切な結果を選択してください:

Aspire CLI — 出力例
Select an integration to add:
> garnet (Aspire.Hosting.Garnet)
> Other results listed as selectable options...

In your AppHost project, call AddGarnet on the builder instance to add a Garnet resource:

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

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

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

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

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

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

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

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

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

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

The Garnet resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Garnet server
PortThe port number the Garnet server is listening on
PasswordThe password for authentication (available when a password parameter is configured)
UriThe connection URI, with the format redis://:{Password}@{Host}:{Port}

Example connection string:

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

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

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

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

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

質問 & 回答コラボレーションコミュニティディスカッション視聴