Lewati ke konten

Get started with the Redis Distributed Caching integration

Konten ini belum tersedia dalam bahasa Anda.

Redis logo

The Redis® distributed caching integration is used to register an IDistributedCache provider backed by a Redis server with the docker.io/library/redis container image.

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

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

Aspire CLI — Tambahkan paket Aspire.Hosting.Redis
aspire add redis

Aspire CLI interaktif; pilih hasil pencarian yang sesuai saat diminta:

Aspire CLI — Contoh keluaran
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 Redis distributed caching integration, install the package:

.NET CLI — Add Aspire.StackExchange.Redis.DistributedCaching package
dotnet add package Aspire.StackExchange.Redis.DistributedCaching

In the Program.cs file of your client-consuming project, call the AddRedisDistributedCache extension to register the required services for distributed caching:

builder.AddRedisDistributedCache(connectionName: "cache");

You can then retrieve the IDistributedCache instance using dependency injection:

public class ExampleService(IDistributedCache cache)
{
// Use cache...
}