Pular para o conteúdo

Get started with the Garnet integration

Este conteúdo não está disponível em sua língua ainda.

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 — Adicionar pacote Aspire.Hosting.Garnet
aspire add garnet

A Aspire CLI é interativa; escolha o resultado adequado quando solicitado:

Aspire CLI — Exemplo de saída
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...
}