Get started with the Azure Cache for Redis integrations

Azure Cache for Redis logo

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.

Note

To follow this guide, you must have created an Aspire solution to work with. To learn how to do that, see Build your first Aspire app.

Set up hosting integration

To begin, install the Aspire Azure Cache for Redis Hosting integration in your Aspire AppHost project:

Install the NuGet package
dotnet add package Aspire.Hosting.Azure.Redis

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();

Caution

When you call AddAzureManagedRedis, it implicitly calls AddAzureProvisioning—which adds support for generating Azure resources dynamically during app startup. The app must configure the appropriate subscription and location. For more information, see Local provisioning: Configuration.

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:

Install the NuGet 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:

builder.AddRedisClient(connectionName: "redis");

Tip

The connectionName parameter must match the name used when adding the Redis resource in the AppHost project.

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");

Tip

The full set of properties that Aspire injects is available in the client integration documentation. For more information, see Azure Cache for Redis Client integration.

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.

See also

Learn about Azure Cache for Redis hosting integration, including provisioning, customization, and configuration. Learn about Azure Cache for Redis client integration, including distributed cache, output cache, and telemetry.