Get started with the Redis integration
Este conteúdo não está disponível em sua língua ainda.
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.
Set up hosting integration
Section titled “Set up hosting integration”To begin, install the Aspire Redis hosting integration in your Aspire AppHost project:
aspire add redisA CLI Aspire é interativa; escolhe o resultado apropriado quando solicitado:
Select an integration to add:
> redis (Aspire.Hosting.Redis)> Other results listed as selectable options...#:package Aspire.Hosting.Redis@*<PackageReference Include="Aspire.Hosting.Redis" Version="*" />Next, in the AppHost project, create instances of Redis resources and pass them to the consuming client projects:
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.
Set up client integration
Section titled “Set up client integration”To get started with the Aspire Redis client integration, install the package:
dotnet add package Aspire.StackExchange.Redis#:package Aspire.StackExchange.Redis@*<PackageReference Include="Aspire.StackExchange.Redis" Version="*" />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...}