Get started with the Azure Cache for Redis integrations
Цей контент ще не доступний вашою мовою.
Azure Cache for Redis provides an in-memory data store based on the Redis software. Redis improves the performance and scalability of an application that uses backend data stores heavily. It’s able to process large volumes of application requests by keeping frequently accessed data in the server memory, which can be written to and read from quickly.
In this introduction, you’ll see how to install and use the Aspire Azure Cache for Redis integrations in a simple configuration. If you already have this knowledge, see Azure Cache for Redis Hosting integration for full reference details.
Set up hosting integration
Section titled “Set up hosting integration”To begin, install the Aspire Azure Cache for Redis Hosting integration in your Aspire AppHost project:
aspire add azure-redisAspire CLI інтерактивний; оберіть відповідний результат пошуку:
Select an integration to add:
> azure-redis (Aspire.Hosting.Azure.Redis)> Other results listed as selectable options...#:package Aspire.Hosting.Azure.Redis@*<PackageReference Include="Aspire.Hosting.Azure.Redis" Version="*" />Next, in the AppHost project, create an Azure Cache for Redis resource and pass it to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddAzureManagedRedis("redis");
builder.AddProject<Projects.ExampleProject>() .WithReference(redis);
// After adding all resources, run the app...
builder.Build().Run();Set up client integration
Section titled “Set up client integration”To get started with the Aspire Azure Cache for Redis client integration, install the 📦 Aspire.StackExchange.Redis NuGet package in the client-consuming project:
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:
builder.AddRedisClient(connectionName: "redis");Use injected Azure Cache for Redis properties
Section titled “Use injected Azure Cache for Redis properties”In the AppHost, when you used the WithReference method to pass an Azure Cache for Redis resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.
Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Host property of a resource called redis becomes REDIS_HOST.
Use the GetValue() method to obtain these environment variables in consuming projects:
string redisHost = builder.Configuration.GetValue<string>("REDIS_HOST");string redisPort = builder.Configuration.GetValue<string>("REDIS_PORT");string redisUri = builder.Configuration.GetValue<string>("REDIS_URI");Use Azure Cache for Redis resources in client code
Section titled “Use Azure Cache for Redis resources in client code”After adding the IConnectionMultiplexer, you can retrieve the connection instance using dependency injection:
public class ExampleService(IConnectionMultiplexer redis){ // Use redis...}For more information, see StackExchange.Redis documentation for examples on using the IConnectionMultiplexer.