The traditional ASP.NET framework relied on the ASP.NET Cache, which was limited to a single server (InProc) and lacked support for multi-server environments. It also didn't allow integration with third-party distributed caches. As a result, developers were forced to rely on inefficient, standalone caching mechanisms.
Microsoft solved these challenges with ASP.NET Core, introducing the IDistributedCache, interface for scalable, multi-server caching solutions. NCache is easy to integrate with IDistributedCache, which gives your ASP.NET Core applications a high-performance, feature-rich caching layer.
The IDistributedCache interface provides a centralized caching mechanism that works across multiple servers, making it ideal for cloud-native, containerized, and microservice-based architectures. While the standard interface defines the contract, NCache provides the actual distributed implementation that ensures data persists across server restarts and scales linearly with traffic.
using Microsoft.Extensions.Caching.Distributed;
using System.Threading;
using System.Threading.Tasks;
public interface IDistributedCache
{
byte[]? Get(string key);
Task<byte[]?> GetAsync(string key, CancellationToken token = default);
void Refresh(string key);
Task RefreshAsync(string key, CancellationToken token = default);
void Remove(string key);
Task RemoveAsync(string key, CancellationToken token = default);
void Set(string key, byte[] value, DistributedCacheEntryOptions options);
Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default);
}
Many caching solutions, like MemoryDistributedCache, are limited to a single server, making them unsuitable for applications that need scalability and high availability. NCache, on the other hand, is a true distributed caching solution designed specifically for .NET applications, ensuring better performance and scalability.
Integrating NCache with your ASP.NET Core application is simple. You can plug-in NCache without modifying existing application logic. This allows you to transition smoothly from in-memory caching to a highly available distributed cache.
Install-Package Alachisoft.NCache.DistributedCache
var builder = WebApplication.CreateBuilder(args);
// Register NCache as the IDistributedCache provider
// Benefits: Multi-server scalability, High Availability, and 100% Native .NET
builder.Services.AddNCacheDistributedCache(options =>
{
options.CacheName = builder.Configuration["NCache:CacheName"];
// NCache settings can be dynamically adjusted in appsettings.json
// ensuring zero-downtime configuration changes
});
var app = builder.Build();
app.MapControllers();
app.Run();
Alternatively, you can configure NCache in the appsettings.json file and load settings dynamically:
{
"NCacheSettings": {
"CacheName": "myCache",
"EnableLogs": true,
"EnableStatistics": true
}
}
This ensures scalability, fault tolerance, and high availability in production environments.
The IDistributedCache interface provides a basic key-value store, which is useful but quite limited in functionality. NCache goes far beyond by offering an extensive, high-performance caching API that enables powerful, distributed caching for enterprise applications.
| Feature | Standard IDistributedCache (Basic) | NCache API (Enterprise) |
|---|---|---|
| Query Flexibility | Key-value lookup only | SQL & LINQ Queries |
| Data Organization | Flat structure | Tags & Named Tags |
| Data Integrity | Independent items | Cache Dependencies (Relationships) |
| Real-time Messaging | Not supported | Pub/Sub Messaging |
| Database Sync | Manual coding required | Auto Read-Through & Write-Through |
| Data Availability | Empty on startup | Cache Loader & Refresher |
By using the NCache API, organizations can overcome the limitations of IDistributedCache, unlocking enhanced performance, flexibility, and enterprise-grade caching for their .NET applications.
Ready to optimize your ASP.NET Core applications with high-performance distributed caching? Explore NCache and take advantage of its powerful features.
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.