跳转到内容

Get started with the Redis integration

此内容尚不支持你的语言。

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 — 添加 Aspire.Hosting.Redis 包
aspire add redis

Aspire CLI 是交互式的;按提示选择合适的搜索结果:

Aspire CLI — 输出示例
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...
}