Key Takeaways
- NCache is an open-source distributed in-memory cache developed natively in .NET and .NET Core that helps such applications to process fast and scale out with ease.
- Caching is a proven technique used to hold and readily use frequently accessed data.
- This article presents a discussion and implementation of how we can work with NCache IDistributedCache provider using .NET 6.
Introduction
In this post, we'll look at how to implement distributed caching in ASP.NET Core using NCache as the cache provider. A cache is a type of high-speed memory that applications use to store frequently accessed data. It reduces the unnecessary database hits since the data being requested is readily available in the cache. Caching is a well-known approach for boosting application performance.
What is distributed caching
A distributed cache is a cache that is shared by several app servers and is frequently managed as a separate service from the app servers that use it.
A distributed cache can go beyond the memory restrictions of a single computer by connecting multiple computers, which is known as a distributed architecture or a distributed cluster, for increased capacity and processing power.
An ASP.NET Core project's efficiency and scalability can be improved by using a distributed cache, especially if the application is hosted by a cloud service or a server farm. In high-data-volume and high-load applications, distributed caches are extremely useful. The distributed design allows for gradual expansion and scalability by adding more computers to the cluster, allowing the cache to expand in sync with the data growth.
The advantages of distributed cache are:
- Cached data is consistent across all web servers. The user gets the same results regardless of which web server serves their request.
- Web server restarts and deployments do not affect cached data. Individual web servers can be added or removed with no effect on the cache.
- Doesn't use local memory.
What is NCache?
NCache is a free, open-source distributed in-memory cache written in.NET and.NET Core. NCache is a distributed cache that saves application data while avoiding costly database trips. It's lightning-fast and linearly scalable. NCache can be utilized to reduce performance constraints caused by data storage and databases, as well as to enable your.NET Core applications to manage high-volume transaction processing (XTP). It can be used both locally and as a distributed cache cluster for ASP.NET Core apps hosted on Azure or other platforms.
Distributed Caching in ASP.NET Core
Using the IDistributedCache interface provided by dotnetcore, we can connect our ASP.NET Core apps to any distributed cache cluster and utilize it for caching as needed. Almost all major cache providers provide implementations for IDistributedCache, which we can register in the IoC container using IServiceCollection.
IDistributedCache Interface
This is the interface required to access the distributed cache objects, which includes synchronous and asynchronous methods. Items can be added, retrieved, and removed from the distributed cache implementation using this interface.
To perform actions on the actual cache, the IDistributedCache Interface provides the following methods.
- Get, GetAsync: Takes a string key and retrieves the cached item from the cache server if found.
- Set, SetAsync: Add items to the cache using a string key.
- Refresh, RefreshAsync: Resets the sliding expiry timeout of an item in the cache based on its key (if any).
- Remove, RemoveAsync: Deletes the cache data based on the key.
Connecting to an NCache cache
First, we need to install NCache on our machine. You can find a step-by-step tutorial on how to set up NCache on your windows machine here.
Once, the setup is complete, we are ready to use the NCache server in our .NET 6 app. Let's create a .NET 6 project to learn the caching operations with NCache.
First, let's install the following NuGet package:
Install-Package NCache.Microsoft.Extensions.Caching.
This package uses NCache to implement the IDistributedCache interface.
We need to add the cache to the ASP.NET dependencies container in the Program.cs file after installing the NuGet package.
// Program.cs
using Alachisoft.NCache.Caching.Distributed;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Add NCache implementation here...
builder.Services.AddNCacheDistributedCache(configuration => {
configuration.CacheName = "myClusteredCache";
configuration.EnableLogs = true;
configuration.ExceptionsEnabled = true;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
We can also configure the cache settings in the appsettings.json file. To achieve this, we can use another overload of the same method.
builder.Services.AddNCacheDistributedCache(Configuration.GetSection("NCacheSettings"));
// appsettings.josn
{
"NCacheSettings": {
"CacheName": "myClusteredCache",
"EnableLogs": true,
"EnableDetailLogs": false,
"ExceptionsEnabled": true,
"OperationRetry": 0,
"operationRetryInterval": 0
}
}



Join the conversation! Your thoughts help the community grow.