Zum Inhalt springen

Get started with the Garnet integration

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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.

In this introduction, you’ll see how to install and use the Aspire Garnet integrations in a simple configuration. If you already have this knowledge, see Garnet hosting integration for full reference details.

To begin, install the Aspire Garnet hosting integration in your Aspire AppHost project:

Aspire CLI — Aspire.Hosting.Garnet Paket hinzufügen
aspire add garnet

Die Aspire CLI ist interaktiv; das passende Suchergebnis wählen, wenn gefragt:

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

Next, in the AppHost project, create instances of Garnet resources and pass them to the consuming client projects:

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

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

Garnet is Redis-compatible, so you use the same Redis client packages. To get started, install the 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...
}