Skip to content

Latest commit

 

History

History
162 lines (109 loc) · 6.08 KB

stackexchange-redis-distributed-caching-integration.md

File metadata and controls

162 lines (109 loc) · 6.08 KB
title description ms.topic ms.date zone_pivot_groups
.NET Aspire Stack Exchange Redis distributed caching integration
This article describes the .NET Aspire Stack Exchange Redis distributed caching integration features and capabilities
how-to
08/12/2024
resp-host

.NET Aspire Stack Exchange Redis distributed caching integration

In this article, you learn how to use the .NET Aspire Stack Exchange Redis distributed caching integration. The Aspire.StackExchange.Redis.DistributedCaching library is used to register an IDistributedCache provider for connecting to Redis server. It enables corresponding health checks, logging and telemetry.

Get started

To get started with the .NET Aspire Stack Exchange Redis distributed caching integration, install the Aspire.StackExchange.Redis.DistributedCaching NuGet package in the client-consuming project, i.e., the project for the application that uses the Stack Exchange Redis distributed caching client.

dotnet add package Aspire.StackExchange.Redis.DistributedCaching
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching"
                  Version="*" />

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

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

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...
}

App host usage

:::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

Configuration

The .NET Aspire Stack Exchange 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("RedisConnection");

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

{
  "ConnectionStrings": {
    "RedisConnection": "localhost:6379"
  }
}

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

Use configuration providers

The .NET Aspire Stack Exchange Redis distributed caching integration supports xref:Microsoft.Extensions.Configuration?displayProperty=fullName. It loads the xref:Aspire.StackExchange.Redis.StackExchangeRedisSettings from configuration by using the Aspire:StackExchange:Redis key. Example :::no-loc text="appsettings.json"::: that configures some of the options:

{
  "Aspire": {
    "StackExchange": {
      "Redis": {
        "ConfigurationOptions": {
          "ConnectTimeout": 3000,
          "ConnectRetry": 2
        },
        "DisableHealthChecks": true,
        "DisableTracing": false
      }
    }
  }
}

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",
    configureOptions: options => options.ConnectTimeout = 3000);

[!INCLUDE integration-health-checks]

The .NET Aspire Stack Exchange Redis distributed caching integration handles the following:

  • Adds the StackExchange.Redis health check, tries to open the connection and throws when it fails.
  • Integrates with the /health HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic

[!INCLUDE integration-observability-and-telemetry]

Logging

The .NET Aspire Stack Exchange Redis Distributed Caching integration uses the following Log categories:

  • Aspire.StackExchange.Redis
  • Microsoft.Extensions.Caching.StackExchangeRedis

Tracing

The .NET Aspire Stack Exchange Redis Distributed Caching integration will emit the following Tracing activities using OpenTelemetry:

  • "OpenTelemetry.Instrumentation.StackExchangeRedis"

Metrics

The .NET Aspire Stack Exchange Redis Distributed Caching integration currently doesn't support metrics by default due to limitations with the StackExchange.Redis library.

See also