Garnet integration
이 콘텐츠는 아직 번역되지 않았습니다.
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.
Hosting integration
Section titled “Hosting integration”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 add garnetAspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:
Select an integration to add:
> garnet (Aspire.Hosting.Garnet)> Other results listed as selectable options...#:package Aspire.Hosting.Garnet@*<PackageReference Include="Aspire.Hosting.Garnet" Version="*" />Add Garnet resource
Section titled “Add Garnet resource”In your AppHost project, call AddGarnet on the builder instance to add a Garnet resource:
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.
Add Garnet resource with data volume
Section titled “Add Garnet resource with data volume”To add a data volume to the Garnet resource, call the WithDataVolume method:
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.
Add Garnet resource with data bind mount
Section titled “Add Garnet resource with data bind mount”To add a data bind mount to the Garnet resource, call the WithDataBindMount method:
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);Add Garnet resource with persistence
Section titled “Add Garnet resource with persistence”To add persistence to the Garnet resource, call the WithPersistence method with either the data volume or data bind mount:
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.
Connection properties
Section titled “Connection properties”When you reference a Garnet resource using WithReference, the following connection properties are made available to the consuming project:
Garnet
Section titled “Garnet”The Garnet resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the Garnet server |
Port | The port number the Garnet server is listening on |
Password | The password for authentication (available when a password parameter is configured) |
Uri | The connection URI, with the format redis://:{Password}@{Host}:{Port} |
Example connection string:
Uri: redis://:p%40ssw0rd1@localhost:6379Hosting integration health checks
Section titled “Hosting integration health checks”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.
Client integration
Section titled “Client integration”Garnet 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 Garnet client
Section titled “Add Garnet 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 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.
Distributed caching and output caching
Section titled “Distributed caching and output caching”Garnet 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.