How to Quickly Use NCache with NHibernate?

Using NCache with your NHibernate application is a multi-step process.

1. Install and configure NCache on app servers

Installing and configuring NCache can be summarized into these steps:

  1. Install NCache on all app server & cache servers
  2. Create a cache cluster
  3. Configure remote clients
  4. Test the cache cluster
  5. For more details, watch a 7-minute NCache video on Five Steps to Getting Started.

2. Configure NCache as Level-2 Provider

To enable NCache as Level-2 provider for NHibernate, add the following lines to your app.config or web.config file:

 1 <hibernate-configuration>
 2     ...
 3     <session-factory>
 4       ...
 5       <property name="cache.provider_class">
 6         Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider, 
 7         Alachisoft.NCache.Integrations.NHibernate.Cache
 8       </property>
 9     </session-factory>
10   </hibernate-configuration> 

3. Specify NCache properties for NHibernate

Now that you've configured NCache to become a Level-2 provider for NHibernate, you need to provide some NCache specific properties. These properties help NCache work with your application.

1   <configSections>
2     ...
3     <section name="ncache"
4    type="Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheSection,
5    Alachisoft.NCache.Integrations.NHibernate.Cache"/>
6   </configSections> 

The above lines specify an "ncache" section for your config file. Now specify NCache properties in this "ncache" section.

1 <ncache>
2     <!—"default" region is used if no other regions are specified. -->
3     <region name="default">
4       <add key="cacheName" value="myCache"/>
5       <add key="enableCacheException" value="false"/>
6     </region>
8     <!— see docs for custom regions & db-synchronization properties. -->
9   </ncache> 

There are more NCache properties if you want to synchronize the cache with your database. See product documentation for more details.


What to Do Next?