Alachisoft NCache 4.1 - Online Documentation

Enabling NCache in Hibernate Applications

 
NCache provides a second level or process level distributed cache for Hibernate by implementing CacheProvider and Cache interfaces. You can plug-in NCache to your existing Hibernate application without any code change. Just change your Hibernate configuration file to plug-in NCache as your cache provider and you're all set to start using distributed caching.
 
Follow these steps to use NCache with Hibernate.
 
  1. Register 'hibernate' and 'ncache' configuration sections in the <configSections> of the web.config or app.config as follows:
     
    <configSections>
    <section name = "hibernate-configuration" type = "Hibernate.Cfg.ConfigurationSectionHandler, Hibernate"/>
    <section name = "ncache" type = "Alachisoft.NCache.Integrations.Hibernate.Cache.NCacheSection, Alachisoft.NCache.Integrations.Hibernate.Cache"/>
    </configSections>
     
  2. Define section details for <hibernate> as follows:
     
    <hibernate-configuration xmlns = "urn:hibernate-configuration-2.2">
    <session-factory>
    <property name = "cache.provider_class"> Alachisoft.NCache.Integrations.Hibernate.Cache.NCacheProvider, Alachisoft.NCache.Integrations.Hibernate.Cache </property>
    </session-factory>
    </hibernate-configuration>
     
  3. Define section details for <ncache> as follows:
     
<ncache>
            <region name = "default">
              <add key = "cacheName" value = "mycache"/>
              <add key = "enableCacheException" value = "false"/>
              <class name = "hibernator.BLL.Customer">
                <add key = "priority" value = "1"/>
                <add key = "useAsync" value = "false"/>
                <add key = "relativeExpiration" value = "180"/> or <add key = "staticExpiration" value = "100"/>
              </class>
            </region>
</ncache>
 
 
The above example of app.config or web.config contains the following options for you to configure. Each of them is explained below as well.
 
  • hibernate.cache.provider_class: This option lets you specify that NCache is your second level cache provider. You need to mention two classes that implement Hibernate.Cache.CacheProvider and Hibernate.Cache.Cache interfaces respectively. This is how Hibernate knows how to call this second level cache.
 
  • The "region" attribute of the NCache determines the Cache in which the particular entity will be cached.
 
  • cacheName: This option is included inside the <ncache> tag and lets you specify name of the cache that you have configured through NCache Manager. All caches in NCache are named and this name allows NCache provider to connect to the right cache.
 
  • enableCacheException: This flag allows you to turn on exception handling which by default is turned off. This will throw an exception which you'll have to catch in your java application.
 
  • class: This lets you specify options specific to a java class. All the options below are class-specific. Class name must be same as specified in the mapping file of the class ( .hbm.xml file).
 
  • priority: The priority you want to use for cached items. The possible values are from 1 to 6.
 
  • useAsync: This is a flag that tells the provider whether to use the standard synchronized "Add" and "Insert" or the asynchronous "AddAsync" and "InsertAsync" operations. The asynchronous operations immediately return control back to the client and queues up the operation in a background queue. Then, a background thread executes the command.
 
  • relativeExpiration: This value is specified as number of seconds. It indicates the idle-time expiration for your items, meaning how long your item can stay without being accessed.
Note:
  • Calling new Configuration() only reads the <hibernate> section.
  • Calling no-parameter version of cfg.Configure() configures Hibernate from app.config if it contains <hibernate-configuration>, otherwise hibernate.cfg.xml is used.
     
     
     
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.