Aspire.StackExchange.Redis.DistributedCaching
8.0.0-preview.2.23619.3
Prefix Reserved
See the version list below for details.
dotnet add package Aspire.StackExchange.Redis.DistributedCaching --version 8.0.0-preview.2.23619.3
NuGet\Install-Package Aspire.StackExchange.Redis.DistributedCaching -Version 8.0.0-preview.2.23619.3
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="8.0.0-preview.2.23619.3" />
paket add Aspire.StackExchange.Redis.DistributedCaching --version 8.0.0-preview.2.23619.3
#r "nuget: Aspire.StackExchange.Redis.DistributedCaching, 8.0.0-preview.2.23619.3"
// Install Aspire.StackExchange.Redis.DistributedCaching as a Cake Addin #addin nuget:?package=Aspire.StackExchange.Redis.DistributedCaching&version=8.0.0-preview.2.23619.3&prerelease // Install Aspire.StackExchange.Redis.DistributedCaching as a Cake Tool #tool nuget:?package=Aspire.StackExchange.Redis.DistributedCaching&version=8.0.0-preview.2.23619.3&prerelease
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, register a Redis container and consume the connection using the following methods:
var redis = builder.AddRedisContainer("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
- https://learn.microsoft.com/aspnet/core/performance/caching/distributed
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
https://github.com/dotnet/aspire
*Redis is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Aspire.StackExchange.Redis (>= 8.0.0-preview.2.23619.3)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 3,072 | 11/12/2024 |
9.0.0-rc.1.24511.1 | 1,251 | 10/15/2024 |
8.2.2 | 8,464 | 10/24/2024 |
8.2.1 | 11,364 | 9/26/2024 |
8.2.0 | 19,799 | 8/29/2024 |
8.1.0 | 17,039 | 7/23/2024 |
8.0.2 | 14,096 | 6/28/2024 |
8.0.1 | 16,966 | 5/21/2024 |
8.0.0 | 376 | 5/21/2024 |
8.0.0-preview.7.24251.11 | 992 | 5/7/2024 |
8.0.0-preview.6.24214.1 | 2,788 | 4/23/2024 |
8.0.0-preview.5.24201.12 | 5,091 | 4/9/2024 |
8.0.0-preview.4.24156.9 | 2,642 | 3/12/2024 |
8.0.0-preview.3.24105.21 | 5,323 | 2/13/2024 |
8.0.0-preview.2.23619.3 | 3,508 | 12/20/2023 |
8.0.0-preview.1.23557.2 | 1,021 | 11/14/2023 |