Skip to content

Latest commit

 

History

History
171 lines (111 loc) · 6.73 KB

stackexchange-redis-distributed-caching-integration.md

File metadata and controls

171 lines (111 loc) · 6.73 KB
title description ms.date zone_pivot_groups
.NET Aspire Redis distributed caching integration
Learn how to use the .NET Aspire Redis distributed caching integration, which includes both hosting and client integrations.
02/05/2025
resp-host

.NET Aspire Redis®* distributed caching integration

[!INCLUDE includes-hosting-and-client]

:::zone pivot="redis"

Learn how to use the .NET Aspire Redis distributed caching integration. The Aspire.StackExchange.Redis.DistributedCaching library is used to register an IDistributedCache provider backed by a Redis server with the docker.io/library/redis container image.

:::zone-end :::zone pivot="garnet"

Learn how to use the .NET Aspire Redis distributed caching integration. The Aspire.StackExchange.Redis.DistributedCaching library is used to register an IDistributedCache provider backed by a Garnet server with the ghcr.io/microsoft/garnet container image.

:::zone-end :::zone pivot="valkey"

Learn how to use the .NET Aspire Redis distributed caching integration. The Aspire.StackExchange.Redis.DistributedCaching library is used to register an IDistributedCache provider backed by a Valkey server with the docker.io/valkey/valkey container image.

:::zone-end

Hosting integration

:::zone pivot="redis"

[!INCLUDE redis-app-host]

:::zone-end :::zone pivot="garnet"

[!INCLUDE garnet-app-host]

:::zone-end :::zone pivot="valkey"

[!INCLUDE valkey-app-host]

:::zone-end

Hosting integration health checks

[!INCLUDE redis-hosting-health-checks]

Client integration

[!INCLUDE redis-distributed-client-nuget]

Add Redis client

In the :::no-loc text="Program.cs"::: file of your client-consuming project, call the xref:Microsoft.Extensions.Hosting.AspireRedisDistributedCacheExtensions.AddRedisDistributedCache%2A extension to register the required services for distributed caching and add a xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache for use via the dependency injection container.

builder.AddRedisDistributedCache(connectionName: "cache");

:::zone pivot="redis"

Tip

The connectionName parameter must match the name used when adding the Redis resource in the app host project. For more information, see Add Redis resource.

:::zone-end :::zone pivot="garnet"

Tip

The connectionName parameter must match the name used when adding the Garnet resource in the app host project. For more information, see Add Garnet resource.

:::zone-end :::zone pivot="valkey"

Tip

The connectionName parameter must match the name used when adding the Valkey resource in the app host project. For more information, see Add Valkey resource.

:::zone-end

You can then retrieve the IDistributedCache instance using dependency injection. For example, to retrieve the cache from a service:

public class ExampleService(IDistributedCache cache)
{
    // Use cache...
}

For more information on dependency injection, see .NET dependency injection.

Add keyed Redis client

There might be situations where you want to register multiple IDistributedCache instances with different connection names. To register keyed Redis clients, call the xref:Microsoft.Extensions.Hosting.AspireRedisDistributedCacheExtensions.AddKeyedRedisDistributedCache* method:

builder.AddKeyedRedisDistributedCache(name: "chat");
builder.AddKeyedRedisDistributedCache(name: "product");

Then you can retrieve the IDistributedCache instances using dependency injection. For example, to retrieve the connection from an example service:

public class ExampleService(
    [FromKeyedServices("chat")] IDistributedCache chatCache,
    [FromKeyedServices("product")] IDistributedCache productCache)
{
    // Use caches...
}

For more information on keyed services, see .NET dependency injection: Keyed services.

Configuration

The .NET Aspire Redis distributed caching integration provides multiple options to configure the Redis connection based on the requirements and conventions of your project.

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddRedisDistributedCache:

builder.AddRedisDistributedCache("cache");

And then the connection string will be retrieved from the ConnectionStrings configuration section:

{
  "ConnectionStrings": {
    "cache": "localhost:6379"
  }
}

For more information on how to format this connection string, see the Stack Exchange Redis configuration docs.

Use configuration providers

[!INCLUDE redis-distributed-client-json-settings]

Use inline delegates

You can also pass the Action<StackExchangeRedisSettings> delegate to set up some or all the options inline, for example to configure DisableTracing:

builder.AddRedisDistributedCache(
    "cache",
    settings => settings.DisableTracing = true);

You can also set up the ConfigurationOptions using the Action<ConfigurationOptions> configureOptions delegate parameter of the AddRedisDistributedCache method. For example to set the connection timeout:

builder.AddRedisDistributedCache(
    "cache",
     null,
     static options => options.ConnectTimeout = 3_000);

[!INCLUDE redis-distributed-client-health-checks-and-diagnostics]

See also

[!INCLUDE redis-trademark]