Aller au contenu

Get started with the Redis integration

Ce contenu n’est pas encore disponible dans votre langue.

Redis logo

Redis® is the world’s fastest data platform for caching, vector search, and NoSQL databases. The Aspire Redis integration enables you to connect to existing Redis instances, or create new instances from .NET with the docker.io/library/redis container image.

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

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

Aspire CLI — Ajouter le package Aspire.Hosting.Redis
aspire add redis

La CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :

Aspire CLI — Exemple de sortie
Select an integration to add:
> redis (Aspire.Hosting.Redis)
> Other results listed as selectable options...

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

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("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 Redis instance on your local machine.

To get started with the Aspire Redis client integration, 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 for use via the dependency injection container:

builder.AddRedisClient(connectionName: "cache");

You can then retrieve the IConnectionMultiplexer instance using dependency injection:

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