NCache as Output Cache Provider
The NCache Output Cache integration provides a distributed backing store for ASP.NET Core Output Cache through the IOutputCacheStore interface. It allows ASP.NET Core applications to store complete HTTP responses in NCache so that cached responses can be shared across multiple application instances in a load-balanced environment.
NCache Output Cache integration can be configured using the following approaches:
- Using appsettings.json (Recommended for Production)
- Using an Action Delegate in Program.cs
Both approaches are supported through AddNCacheOutputCacheProvider overloads.
Note
This feature is supported only in the NCache OpenSource edition.
Important
Output Cache is supported in NCache OSS 5.3.6.2 and later.
Note
Sliding expiration is currently not supported.
Prerequisites
Before configuring ASP.NET Core Output Cache with NCache, ensure that the following prerequisites are fulfilled:
- To configure ASP.NET Core Output Cache with NCache, install the following NuGet package in your .NET application:
- OpenSource: NCache.OSS.AspNetCore.OutputCaching
- To utilize the Output Cache provider, include the following namespaces in your application in Program.cs:
NCache.OSS.AspNetCore.OutputCaching
- The cache must be running.
Method 1: Using appsettings.json
This approach stores Output Cache provider settings inside appsettings.json and binds them automatically through IConfiguration during application startup. It is useful for environments where configuration values may change between deployments because cache settings can be modified without changing application code. This is the recommended approach for production environments because configuration changes can be made without recompiling the application.
In this configuration approach, NCache connection settings are defined inside appsettings.json while AddNCacheOutputCacheProvider reads and binds the configuration section automatically during service registration.
Step 1: Configure NCache Settings
The following configuration defines the NCache cache name, logging settings, and server connectivity information used by the Output Cache provider. Add the following configuration section in your appsettings.json file.
{
"NCache": {
"CacheName": "demoCache",
"EnabledLogs": false,
"EnableDetailLogs": false,
"ServerList": [
{
"Ip": "20.200.20.39",
"Port": 9800
}
]
}
}
Step 2: Register the Configuration Section
After defining the NCache settings in appsettings.json, register Output Cache, bind the NCache provider to the NCache configuration section, and enable Output Cache in the request pipeline. The endpoint uses CacheOutput() so its response can be stored and served from NCache.
Add the following code in your Program.cs file.
var builder = WebApplication.CreateBuilder(args);
// Configure Output Cache policies
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(policy =>
policy.Expire(TimeSpan.FromSeconds(30)));
});
// Register NCache Output Cache provider using IConfiguration binding
builder.Services.AddNCacheOutputCacheProvider(
builder.Configuration.GetSection("NCache"));
var app = builder.Build();
// Enable Output Cache middleware
app.UseOutputCache();
// Configure a cacheable endpoint
app.MapGet("/", () => "Cached response")
.CacheOutput();
app.Run();
Method 2: Using an Action Delegate in Program.cs
In this approach, the Output Cache provider configuration is defined directly inside Program.cs through an action delegate passed to AddNCacheOutputCacheProvider. The action delegate allows cache connection settings, logging behavior, and server connectivity to be configured programmatically during application initialization.
The following configuration registers ASP.NET Core Output Cache middleware, configures NCache as the distributed Output Cache provider, enables Output Cache in the request pipeline, and applies Output Cache to an endpoint using CacheOutput().
Add the following code in your Program.cs file.
var builder = WebApplication.CreateBuilder(args);
// Configure Output Cache policies
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(policy =>
policy.Expire(TimeSpan.FromSeconds(30)));
});
// Register NCache as the distributed Output Cache provider
builder.Services.AddNCacheOutputCacheProvider(options =>
{
options.CacheName = "demoCache";
options.EnabledLogs = false;
options.EnableDetailLogs = false;
// Configure NCache server connectivity
options.ServerList = new List<NCacheOutputCacheOptions.ServerConfig>
{
new NCacheOutputCacheOptions.ServerConfig
{
Ip = "20.200.20.39",
Port = 9800
}
};
});
var app = builder.Build();
// Enable Output Cache middleware
app.UseOutputCache();
// Configure a cacheable endpoint
app.MapGet("/", () => "Cached response")
.CacheOutput();
app.Run();
Configure Multiple Output Cache Policies
ASP.NET Core Output Cache allows multiple cache policies to be configured with different expiration settings. These policies can then be applied to specific endpoints based on application requirements.
The following example updates the existing AddOutputCache configuration to define two cache policies with different expiration durations.
// Configure multiple Output Cache policies with different expiration durations
builder.Services.AddOutputCache(options =>
{
// Define a short-duration cache policy (10 seconds)
options.AddPolicy("short", policy =>
policy.Expire(TimeSpan.FromSeconds(10)));
// Define a long-duration cache policy (5 minutes)
options.AddPolicy("long", policy =>
policy.Expire(TimeSpan.FromMinutes(5)));
});
Configuration Properties
The following configuration properties are available in the NCacheOutputCacheOptions class.
Note
Properties marked with an asterisk (*) are required. All other properties are optional.
| Property | Description |
|---|---|
| CacheName* | Specifies the name of the NCache cache instance. The cache must already exist in the NCache cluster and is required for provider initialization. |
| ServerList | Specifies the list of NCache server nodes used for cache connectivity. Each ServerConfig entry contains an IP address and port number. IP addresses must be valid IPv4 or IPv6 addresses and port values must be between 1–65535. |
| EnabledLogs | Enables NCache internal logging. When enabled, initialization and runtime errors are logged through the internal NCache logger. |
| EnableDetailLogs | Controls internal logging verbosity. When enabled, verbose logging is generated. Otherwise, only informational logs are generated. |