Introduction
In high-performance enterprise applications, caching plays a vital role in improving responsiveness and scalability. When dealing with high transaction loads or frequent data access, distributed caching helps offload pressure from databases and boosts throughput.
However, as your cache grows and serves multiple APIs or microservices, real-time monitoring and alerting become crucial for stability and performance visibility.
That’s where NCache—a powerful open-source distributed cache for .NET—comes into play. It not only accelerates your applications but also provides real-time monitoring, dashboards, and alerts for cache health and performance metrics.
In this article, we’ll build an ASP.NET Core Web API integrated with NCache and configure real-time cache monitoring and alerting.
What is NCache?
NCache is a 100% native .NET distributed caching solution by Alachisoft. It provides:
High availability through replication and partitioning
In-memory data storage for low latency
Real-time monitoring and management
Built-in notifications and alerts
It’s widely used in .NET Core microservices, Web APIs, SignalR, and Azure/AWS deployments.
Why Monitor Cache in Real-Time?
Real-time cache monitoring helps you:
Detect cache node failures or disconnections immediately
Track hit/miss ratios to optimize caching logic
Identify memory pressure or expired items
Receive alerts before performance degradation impacts users
Setting Up the ASP.NET Core Web API
Let’s create a new ASP.NET Core project and integrate NCache.
Step 1. Create the Project
dotnet new webapi -n NCacheMonitoringDemo
cd NCacheMonitoringDemo
Step 2. Add Required Packages
Install the following NuGet packages:
dotnet add package Alachisoft.NCache.SDK
dotnet add package Alachisoft.NCache.SessionState
dotnet add package Alachisoft.NCache.Runtime
Step 3. Configure NCache in Program.cs
Here’s how you configure NCache in your .NET 8 Web API:
using Alachisoft.NCache.Client;
using Alachisoft.NCache.Runtime.Caching;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Configure NCache
builder.Services.AddSingleton<ICache>(provider =>
{
string cacheName = "demoClusteredCache";
var cache = CacheManager.GetCache(cacheName);
return cache;
});
var app = builder.Build();
app.MapControllers();
app.Run();
This connects your API to a clustered cache named demoClusteredCache running on your NCache server(s).
Step 4. Using the Cache in Your Controller
Let’s create a simple way ProductController to interact with NCache.
using Microsoft.AspNetCore.Mvc;
using Alachisoft.NCache.Client;
using Alachisoft.NCache.Runtime.Caching;
namespace NCacheMonitoringDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
private readonly ICache _cache;
public ProductController(ICache cache)
{
_cache = cache;
}
[HttpPost]
public IActionResult AddProduct([FromBody] Product product)
{
_cache.Insert(product.Id.ToString(), product);
return Ok("Product cached successfully!");
}
[HttpGet("{id}")]
public IActionResult GetProduct(string id)
{
var product = _cache.Get<Product>(id);
if (product == null)
return NotFound("Product not found in cache!");
return Ok(product);
}
}
public record Product(string Id, string Name, decimal Price);
}
Now, whenever you call POST /api/product, your data gets cached in NCache, and can be retrieved with GET /api/product/{id}.
Join the conversation! Your thoughts help the community grow.