Get started with the Redis Output Caching integration
Konten ini belum tersedia dalam bahasa Anda.
The Redis® output caching integration is used to register an ASP.NET Core Output Caching 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 Output Caching integrations in a simple configuration. If you already have this knowledge, see Redis Output 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 output caching integration, install the package:
dotnet add package Aspire.StackExchange.Redis.OutputCaching#:package Aspire.StackExchange.Redis.OutputCaching@*<PackageReference Include="Aspire.StackExchange.Redis.OutputCaching" Version="*" />In the Program.cs file of your client-consuming project, call the AddRedisOutputCache extension method to register the required services for output caching:
builder.AddRedisOutputCache(connectionName: "cache");Add the middleware to the request processing pipeline by calling UseOutputCache:
var app = builder.Build();
app.UseOutputCache();For minimal API apps, configure an endpoint to do caching by calling CacheOutput, or by applying the OutputCacheAttribute:
app.MapGet("/cached", () => "Hello world!") .CacheOutput();
app.MapGet( "/attribute", [OutputCache] () => "Hello world!");For apps with controllers, apply the [OutputCache] attribute to the action method. For Razor Pages apps, apply the attribute to the Razor page class.