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. Below is the latest interface definition for .NET 8:
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.
To use NCache as your default IDistributedCache provider, simply register it in the ASP.NET Core applications Program.cs file:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddNCacheDistributedCache(options =>
{
options.CacheName = builder.Configuration["NCache:CacheName"];
});
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.
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.