Get started with the 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.

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.

Note

To follow this guide, you must have created an Aspire solution to work with. To learn how to do that, see Build your first Aspire app.

Set up hosting integration

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

Install the NuGet package
dotnet add package Aspire.Hosting.Garnet

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

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.

Tip

If you'd rather connect to an existing Garnet instance, call AddConnectionString instead. For more information on how to use WithReference, see Resource dependencies.

Set up client integration

Garnet is Redis-compatible, so you use the same Redis client packages. To get started, install the package:

Install the NuGet 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");

Tip

The connectionName parameter must match the name used when adding the Garnet resource in the AppHost project.

You can then retrieve the IConnectionMultiplexer instance using dependency injection:

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

See also