Ir al contenido

Get started with the Redis integration

Esta página aún no está disponible en tu idioma.

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 — Añadir paquete Aspire.Hosting.Redis
aspire add redis

La CLI de Aspire es interactiva; asegúrate de seleccionar el resultado adecuado cuando se te pida:

Aspire CLI — Ejemplo de salida
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...
}