Redis Distributed Caching
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.
Hosting integration
Section titled “Hosting integration”The Redis hosting integration models a Redis resource as the RedisResource type. To access this type and APIs, add the 📦 Aspire.Hosting.Redis NuGet package in your AppHost project:
aspire add redisThe Aspire CLI is interactive, be sure to select the appropriate search result when prompted:
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="*" />Add Redis resource
Section titled “Add Redis resource”In your AppHost project, call AddRedis on the builder instance to add a Redis resource:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var myService = builder.AddProject<Projects.ExampleProject>() .WithReference(cache);When Aspire adds a container image to the AppHost, it creates a new Redis instance on your local machine.
Add Redis resource with data volume
Section titled “Add Redis resource with data volume”To add a data volume to the Redis resource, call the WithDataVolume method:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache") .WithDataVolume(isReadOnly: false);
var myService = builder.AddProject<Projects.ExampleProject>() .WithReference(cache);The data volume is used to persist the Redis data outside the lifecycle of its container. The data volume is mounted at the /data path in the Redis container.
Add Redis resource with persistence
Section titled “Add Redis resource with persistence”To add persistence to the Redis resource, call the WithPersistence method with either the data volume or data bind mount:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache") .WithDataVolume() .WithPersistence( interval: TimeSpan.FromMinutes(5), keysChangedThreshold: 100);
var myService = builder.AddProject<Projects.ExampleProject>() .WithReference(cache);The preceding code adds persistence to the Redis resource by taking snapshots of the Redis data at a specified interval and threshold.
For more Redis hosting integration options, see the main Redis integration page.
Client integration
Section titled “Client integration”To get started with the Redis distributed caching integration, install the 📦 Aspire.StackExchange.Redis.DistributedCaching NuGet package:
dotnet add package Aspire.StackExchange.Redis.DistributedCaching#:package Aspire.StackExchange.Redis.DistributedCaching@*<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="*" />Add Redis distributed cache
Section titled “Add Redis distributed cache”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...}Add keyed Redis distributed cache
Section titled “Add keyed Redis distributed cache”Due to its limitations, you cannot register multiple IDistributedCache instances simultaneously. However, there may be scenarios where you need to register multiple Redis clients. To register a keyed Redis client for IDistributedCache, call the AddKeyedRedisDistributedCache method:
builder.AddKeyedRedisClient(name: "chat");builder.AddKeyedRedisDistributedCache(name: "product");Then retrieve the instances:
public class ExampleService( [FromKeyedServices("chat")] IConnectionMultiplexer chatConnectionMux, IDistributedCache productCache){ // Use product cache...}Configuration
Section titled “Configuration”Use a connection string
Section titled “Use a connection string”When using a connection string from the ConnectionStrings configuration section:
builder.AddRedisDistributedCache("cache");Then the connection string will be retrieved:
{ "ConnectionStrings": { "cache": "localhost:6379" }}Use configuration providers
Section titled “Use configuration providers”The Redis distributed caching integration supports Microsoft.Extensions.Configuration. Example appsettings.json:
{ "Aspire": { "StackExchange": { "Redis": { "DistributedCaching": { "ConnectionString": "localhost:6379", "DisableHealthChecks": false, "DisableTracing": false } } } }}Use inline delegates
Section titled “Use inline delegates”You can pass the delegate to set up options inline:
builder.AddRedisDistributedCache( "cache", settings => settings.DisableTracing = true);You can also configure the ConfigurationOptions:
builder.AddRedisDistributedCache( "cache", null, static options => options.ConnectTimeout = 3_000);Client integration health checks
Section titled “Client integration health checks”By default, Aspire integrations enable health checks. The Redis distributed caching integration adds a health check that verifies the Redis instance is reachable.
Observability and telemetry
Section titled “Observability and telemetry”Logging
Section titled “Logging”The Redis distributed caching integration uses standard .NET logging.
Tracing
Section titled “Tracing”The integration emits tracing activities using OpenTelemetry.
Metrics
Section titled “Metrics”The integration emits metrics using OpenTelemetry.