NHibernate is a popular Object-Relational Mapping (ORM) framework for .NET applications, which enables seamless database interactions through object-oriented code. However, when applications handle a high volume of transaction loads, they often experience performance bottlenecks due to frequent database queries.
To address this, NHibernate provides Second-Level Caching, which keeps objects in memory at the application level, reducing the need to repeatedly access the database. NCache, an extremely fast and scalable distributed cache, acts as an NHibernate Second-Level Cache provider, which significantly improves application speed, scalability, and reliability.
NCache provides a high-performance, distributed Second Level (L2) Cache for NHibernate that eliminates database bottlenecks in .NET applications. It offers linear scalability, 100% uptime through peer-to-peer clustering, and requires zero code changes for integration.
NCache boosts NHibernate with faster queries, reduced database load, and high-availability as explained below.
| Feature | NHibernate L1 (Session Cache) | NCache (Distributed L2 Cache) |
|---|---|---|
| Scope & Isolation | Transaction-scoped. Data is isolated to a single ISession object. | Process-wide/Cluster-wide. Data is shared across all sessions and all servers in the web farm. |
| Data Persistence | Ephemeral. Objects are disposed of as soon as the session is closed or the request ends. | Persistent In-Memory. Data lives in the cache cluster independently of application restarts or session closures. |
| Scalability | In-process only. Limited by the memory of a single web server; cannot be shared across a farm. | Linear Scalability. You can add cache servers dynamically to increase storage capacity and throughput. |
| Database Sync | None. L1 has no mechanism to know if another process updated the DB. | Automatic Synchronization. Uses SqlCacheDependency, Oracle Dependency, or Polling to invalidate stale data. |
| Query Caching | Not applicable (L1 is primarily for ID-based lookups). | Supported. Caches results of complex queries to avoid repeated expensive SQL executions. |
Following are some of the key features that NCache offers to NHibernate users:
<configuration>
<application-config application-id="myapp"
enable-cache-exception="true"
default-region-name="default"
key-case-sensitivity="false" >
<cache-regions>
<region name="AbsoluteExpirationRegion"
cache-name="demoCache"
priority="Default"
expiration-type="sliding"
expiration-period="180" />
<region name="default"
cache-name="demoCache"
priority="default"
expiration-type="none"
expiration-period="0"/>
</cache-regions>
</application-config>
</configuration>
<configuration>
<application-config application-id="myapp"
enable-cache-exception="true"
default-region-name="default"
key-case-sensitivity="false">
<database-dependencies>
<dependency entity-name="nhibernator.BLL.Customer"
type="sql"
sql-statement="SELECT ContactName FROM dbo.Customers WHERE CustomerID =?"
cache-key-format="dependency.customer: [us]"/>
</database-dependencies>
</application-config>
</configuration>
This example shows how to enable NHibernate query caching with NCache to store and reuse query results for improved performance.
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
IQuery query = session.CreateQuery("FROM Customer c")
.SetCacheable(true);
var customers = await query.ListAsync<Customer>();
var newCustomer = new Customer { Name = "John Doe" };
await session.SaveAsync(newCustomer);
await session.FlushAsync();
transaction.Commit();
}
}
Install-Package NHibernate.NCache
Modify your NHibernate configuration files to enable NCache as the second-level cache provider.
Update hibernate.cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="cache.provider_class">
Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider,
Alachisoft.NCache.Integrations.NHibernate.Cache
</property>
<property name="cache.use_second_level_cache">true</property>
</session-factory>
</hibernate-configuration>
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.