Aspire.StackExchange.Redis.OutputCaching
8.0.1
Prefix Reserved
See the version list below for details.
dotnet add package Aspire.StackExchange.Redis.OutputCaching --version 8.0.1
NuGet\Install-Package Aspire.StackExchange.Redis.OutputCaching -Version 8.0.1
<PackageReference Include="Aspire.StackExchange.Redis.OutputCaching" Version="8.0.1" />
paket add Aspire.StackExchange.Redis.OutputCaching --version 8.0.1
#r "nuget: Aspire.StackExchange.Redis.OutputCaching, 8.0.1"
// Install Aspire.StackExchange.Redis.OutputCaching as a Cake Addin #addin nuget:?package=Aspire.StackExchange.Redis.OutputCaching&version=8.0.1 // Install Aspire.StackExchange.Redis.OutputCaching as a Cake Tool #tool nuget:?package=Aspire.StackExchange.Redis.OutputCaching&version=8.0.1
Aspire.StackExchange.Redis.OutputCaching library
Registers an ASP.NET Core Output Caching provider backed by a Redis®* server. 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 OutputCache library with NuGet:
dotnet add package Aspire.StackExchange.Redis.OutputCaching
Usage example
In the Program.cs file of your project, call the AddRedisOutputCache
extension method to register the Redis output cache provider in the dependency injection container. The method takes a connection name parameter.
builder.AddRedisOutputCache("cache");
After the WebApplication
has been built, add the middleware to the request processing pipeline by calling UseOutputCache.
app.UseOutputCache();
For minimal API apps, configure an endpoint to do caching by calling CacheOutput, or by applying the [OutputCache]
attribute, as shown in the following examples:
app.MapGet("/cached", Gravatar.WriteGravatar).CacheOutput();
app.MapGet("/attribute", [OutputCache] (context) =>
Gravatar.WriteGravatar(context));
For apps with controllers, apply the [OutputCache]
attribute to the action method. For Razor Pages apps, apply the attribute to the Razor page class.
Configuration
The .NET Aspire StackExchange Redis OutputCache 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.AddRedisOutputCache()
:
builder.AddRedisOutputCache("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 OutputCache 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
},
"DisableHealthChecks": true,
"DisableTracing": false
}
}
}
}
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.AddRedisOutputCache("cache", settings => settings.DisableHealthChecks = true);
You can also setup the ConfigurationOptions using the Action<ConfigurationOptions> configureOptions
delegate parameter of the AddRedisOutputCache
method. For example to set the connection timeout:
builder.AddRedisOutputCache("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.AddRedisOutputCache("cache");
Additional documentation
- https://learn.microsoft.com/aspnet/core/performance/caching/output
- 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.1)
- Microsoft.AspNetCore.OutputCaching.StackExchangeRedis (>= 8.0.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Aspire.StackExchange.Redis.OutputCaching:
Package | Downloads |
---|---|
SubUtil.DataAccess.SqlServerDataAccessLibrary
Package Description |
|
SubUtil.DataAccess.PostgreSqlDataAccessLibrary
Package Description |
GitHub repositories (6)
Showing the top 5 popular GitHub repositories that depend on Aspire.StackExchange.Redis.OutputCaching:
Repository | Stars |
---|---|
dotnet/aspire
Tools, templates, and packages to accelerate building observable, production-ready apps
|
|
microsoft/fluentui-blazor
Microsoft Fluent UI Blazor components library. For use with ASP.NET Core Blazor applications
|
|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
|
dotnet/aspire-samples
|
|
dotnet/dotnet
Home of .NET's Virtual Monolithic Repository which includes all the code needed to build the .NET SDK from source
|
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 5,612 | 11/12/2024 |
9.0.0-rc.1.24511.1 | 2,048 | 10/15/2024 |
8.2.2 | 3,999 | 10/24/2024 |
8.2.1 | 4,862 | 9/26/2024 |
8.2.0 | 12,494 | 8/29/2024 |
8.1.0 | 17,145 | 7/23/2024 |
8.0.2 | 4,745 | 6/28/2024 |
8.0.1 | 9,072 | 5/21/2024 |
8.0.0 | 13,926 | 5/21/2024 |
8.0.0-preview.7.24251.11 | 1,564 | 5/7/2024 |
8.0.0-preview.6.24214.1 | 4,474 | 4/23/2024 |
8.0.0-preview.5.24201.12 | 2,335 | 4/9/2024 |
8.0.0-preview.4.24156.9 | 5,144 | 3/12/2024 |
8.0.0-preview.3.24105.21 | 4,872 | 2/13/2024 |
8.0.0-preview.2.23619.3 | 4,976 | 12/20/2023 |
8.0.0-preview.1.23557.2 | 7,033 | 11/14/2023 |