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:

<hibernate - configuration> 
    ...
    <session - factory> 
    ...
    <property name = "cache.provider_class"> 
    Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider, 
    Alachisoft.NCache.Integrations.NHibernate.Cache
    </property> 
    </session - factory> 
</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.

<configSections> 
    ...
    <section name = "ncache" 
    type = "Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheSection, 
    Alachisoft.NCache.Integrations.NHibernate.Cache"/> 
</configSections> 

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

<ncache>
    <!--default regionis  used if  no other regions are specified.-->
    <region name = " default ">
        <add key = " cacheName "  value = " myCache " />
        <add key = " enableCacheException "  value = " false " />
    </region>
    <!--see docs for  custom regions  &  db - synchronization properties.-->
</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?

Signup for monthly email newsletter to get latest updates.

© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.