Entity Framework Core (EF Core) is widely used in .NET Core applications for database interactions. However, it has some limitations; high-transaction EF Core applications are likely to experience performance issues at the database tier because of too many database queries. The application tier can be scaled horizontally, but the database tier is a bottleneck.
NCache, a 100% native .NET distributed cache, enhances EF Core applications by caching frequently accessed data and offloading database queries. This improves performance, scalability and reduces the overall load on the database.
NCache extends EF Core with C# extension methods, allowing seamless caching without modifying existing logic.
Transactional data includes frequently changing records such as customer details, orders, and transactions. Caching this data reduces database queries, speeds up response times, and minimizes contention. (Short-term caching for frequently accessed data).
Example:
var customers = context.Customers
.Where(c => c.City == "New York")
.FromCache(options);
Reference data includes relatively static datasets such as product catalogs and lookup values. Caching this data enhances application speed by eliminating redundant database queries. (Long-term caching for static datasets)
Example:
context.Products
.Where(p => p.InStock == true)
.LoadIntoCache(out string cacheKey, options);
Retrieving reference data directly from the cache allows applications to avoid database queries, improving response time. However, the entire dataset must be preloaded in the cache to ensure accurate results and avoid incomplete query responses. (Retrieving data directly from cache without database calls).
Example:
var discontinuedProducts = context.Products
.Where(p => p.Discontinued == true)
.FromCacheOnly();
Note: Ensure the entire dataset is cached to maintain query accuracy.
NCache provides a direct API for managing cached entities efficiently without relying solely on EF Core extension methods. With its caching API, developers can insert, update, and remove objects in the cache programmatically, giving them fine control over caching behavior while optimizing data retrieval for high-performance applications.
Example - Inserting an entity into cache:
var cache = context.GetCache();
cache.Insert(new Product { Id = 101, Name = "Laptop" }, out string cacheKey, options);
Deferred query execution allows applications to perform aggregate functions like Count (), Sum (), and Average () directly on cached data instead of querying the database repeatedly. This reduces redundant queries, improves performance, and speeds up data retrieval in high-transaction environments.
Example:
int pendingOrders = context.Orders
.Where(o => o.Status == "Pending")
.DeferredCount()
.FromCache(options);
NCache allows applications to execute LINQ queries directly on cached data, which eliminates the need for repeated database queries. By leveraging in-memory processing, this approach significantly improves query performance and reduces database load.
Example:
var cachedProducts = context.Products
.Where(p => p.Category == "Beverages")
.FromCacheOnly();
NCache provides caching options that allow developers to customize cache behavior based on application requirements. This enables better control over data storage, setting expiration policies, enabling SQL dependency tracking for auto-invalidation, and defining how data is stored in the cache for efficient retrieval.
Example:
var options = new CachingOptions
{
QueryIdentifier = "CustomerData",
CreateDbDependency = true,
StoreAs = StoreAs.SeparateEntities
};
options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(5));
NCache includes built-in logging and monitoring features to track cache usage, measure performance metrics, and detect potential issues in real-time. It integrates seamlessly with Microsoft.Extensions.Logging, which allows developers to analyze cache behavior, troubleshoot problems, and optimize performance efficiently.
Install-Package EntityFrameworkCore.NCache
public partial class NorthwindContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string cacheId = "myCache";
var options = new CacheConnectionOptions { /* options here */ };
NCacheConfiguration.Configure(cacheId, DependencyType.SqlServer, options);
optionsBuilder.UseSqlServer("your_connection_string");
}
}
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.