Skip to main content
net8.0

A Redis® implementation for IDistributedCache that integrates with Aspire, including health checks, logging, and telemetry.

Published
Apr 9, 2024
2y ago
Total Downloads
3.6M
3,593,331

Deprecated Package Version

Reason: Other, Legacy

This version is out of support and is no longer maintained. Please upgrade to the latest version. See our support policy for details: https://aka.ms/dotent/aspire/support

Install

dotnet add package Aspire.StackExchange.Redis.DistributedCaching --version 8.0.0-preview.5.24201.12
ProductVersions
.NET
net8.0

README

Aspire.StackExchange.Redis.DistributedCaching library

Registers an IDistributedCache in the DI container that connects to a Redis®* server. See Distributed Caching for more information. Enables corresponding health check, logging, and telemetry.

Getting started

Prerequisites

  • Redis server and the server hostname for connecting a client.

Install the package

Install the .NET Aspire StackExchange Redis Distributed Cache library with NuGet:

dotnet add package Aspire.StackExchange.Redis.DistributedCaching

Usage example

In the Program.cs file of your project, call the AddRedisDistributedCache extension method to register an IDistributedCache for use via the dependency injection container. The method takes a connection name parameter.

builder.AddRedisDistributedCache("cache");

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

private readonly IDistributedCache _cache;

public ProductsController(IDistributedCache cache)
{
    _cache = cache;
}

Configuration

The .NET Aspire StackExchange Redis Distributed Cache component provides multiple options to configure the Redis connection based on the requirements and conventions of your project. Note that at least one host name is required to connect.

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

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

{
  "ConnectionStrings": {
    "myRedisConnectionName": "localhost:6379"
  }
}

See the Basic Configuration Settings of the StackExchange.Redis docs for more information on how to format this connection string.

Use configuration providers

The Redis Distributed Cache component supports Microsoft.Extensions.Configuration. It loads the StackExchangeRedisSettings and ConfigurationOptions from configuration by using the Aspire:StackExchange:Redis key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "StackExchange": {
      "Redis": {
        "ConfigurationOptions": {
          "ConnectTimeout": 3000,
          "ConnectRetry": 2
        },
        "HealthChecks": false,
        "Tracing": true
      }
    }
  }
}

Use inline delegates

You can also pass the Action<StackExchangeRedisSettings> configureSettings delegate to set up some or all the options inline, for example to disable health checks from code:

builder.AddRedisDistributedCache("cache", settings => settings.HealthChecks = false);

You can also setup 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);

AppHost extensions

In your AppHost project, install the Aspire.Hosting.Redis library with NuGet:

dotnet add package Aspire.Hosting.Redis

Then, in the Program.cs file of AppHost, register a Redis server and consume the connection using the following methods:

var redis = builder.AddRedis("cache");

var myService = builder.AddProject<Projects.MyService>()
                       .WithReference(redis);

The WithReference method configures a connection in the MyService project named cache. In the Program.cs file of MyService, the redis connection can be consumed using:

builder.AddRedisDistributedCache("cache");

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

*Redis is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd.

Changes from Previous Version

No structural changes between v8.0.0-preview.4.24156.9 and v8.0.0-preview.5.24201.12.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.NuGet marks this package as deprecated. Review the publisher's migration guidance before adopting it.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.