Get started with the Redis Distributed Caching integration
Konten ini belum tersedia dalam bahasa Anda.
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.
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 redisAspire CLI interaktif; pilih hasil pencarian yang sesuai saat diminta:
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 Redis distributed caching integration, install the package:
dotnet add package Aspire.StackExchange.Redis.DistributedCaching#:package Aspire.StackExchange.Redis.DistributedCaching@*<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="*" />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...}