Modern web applications require fast performance and scalability. As applications grow, frequent database queries can slow things down, leading to performance bottlenecks, and reduced responsiveness.
ASP.NET Core is a powerful framework for building scalable web applications, but it performs well when combined with a distributed caching solution like NCache. Caching frequently accessed data reduces database load, increases the response time, and improves scalability.
Let's discuss the benefits of distributed caching, its real-world use cases, and how to integrate NCache with ASP.NET Core applications.
Distributed caching stores frequently used data across multiple servers, making a scalable and reliable in-memory data store. This helps reduce latency, minimizes database queries, and improves application responsiveness.
Installing NCache Server
Install NCache Server on two servers from the official website. Use NCache Manager to create and start a clustered cache for high availability and performance.
Installing NCache Client SDK
To connect an ASP.NET Core application to the cache server, install the NCache Client SDK using NuGet:
dotnet add package Alachisoft.NCache.SDK
Ensure your network and firewall settings allow communication between your application and the cache server.
A clustered cache improves scalability and ensures high availability by distributing cached data across multiple servers.
After setting up NCache, configure your ASP.NET Core application to connect to the cache.
Program.cs (Modify)
using Alachisoft.NCache.Client;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ICache>(_ => CacheManager.GetCache("demoCache"));
var app = builder.Build();
app.Run();
public Dictionary<string, Product> RetrieveProducts(List<int> productIds)
{
List<string> cacheKeys = productIds.Select(id => $"Product:{id}").ToList();
Dictionary<string, Product> cachedProducts = _cache.GetBulk<Product>(cacheKeys);
return cachedProducts;
}
public Product GetProduct(int productId)
{
string cacheKey = $"Product:{productId}";
Product product = _cache.Get<Product>(cacheKey);
if (product == null)
{
product = FetchProductFromDB(productId);
var cacheItem = new CacheItem(product)
{
Expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(10))
};
_cache.Add(cacheKey, cacheItem);
}
return product;
}
public void UpdateProduct(int productId, Product updatedProduct)
{
// Update the database first
UpdateProductInDB(productId, updatedProduct);
// Update the cache after successful database update
string cacheKey = $"Product:{productId}";
var cacheItem = new CacheItem(updatedProduct)
{
Expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(10))
};
_cache.Insert(cacheKey, cacheItem);
}
public void RemoveData(List<string> keys)
{
_cache.RemoveBulk(keys);
}
NCache offers additional enterprise-level caching features to enhance performance:
Optimize your ASP.NET Core applications with NCache! Reduce database load, enhance performance, and scale effortlessly.
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.