NHibernate with NCache
NHibernate has a first level or session level cache which is used by default. Each session contains its own session level cache which is not shared with other sessions. This cache is disposed whenever the session is closed. However, NHibernate allows to use second level, across session cache which spans across sessions’ life time and can be shared by multiple sessions.
NCache provides a second level or process level cache for NHibernate by
implementing NHibernate's ICacheProvider
and ICache
interfaces. This enables
NCache to be used as NHibernate second cache in existing NHibernate application
without any code change. All that’s needed is to change NHibernate configuration
file to plug-in NCache as cache provider and distributed caching can be used.
Second level cache can be configured by enabling the use of second level cache and configuring second level cache provider in NHibernate configurations.
Configuring NHibernate Application to use NCache as Second Level Cache
Create NHibernate region in Web.config
Add the <hibernate-configuration>
in Web.config under the <configSections>
tag:
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
Specify Configurations for NHibernate Region
Place NCache’s NHibernate Provider assembly
Alachisoft.NCache.Integrations.NHibernate.Cache.dll
located at[InstallDir]/integration/nhibernate/assembly/
in the application's base directory.Configure NCache as NHibernate's second level cache provider by adding the
cache.provider_class
property under the specified region (<hibernate-configuration>
).Enable the use of second level cache in application by setting the
cache.use_second_level_cache
property totrue
.
<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>
Specify Application ID for NCache NHibernate Provider
NCache Provider for NHibernate identifies each application by an application ID. This ID is used to find the appropriate configuration for that application from NCache configuration file for NHibernate (NCacheNHibernate.xml). This application id must be specified in application's configuration file (Web.config):
<appSettings>
<add key="ncache.application_id" value="myapp" />
</appSettings>
Configuring Cache Regions
NHibernate uses cache regions to store objects. NCache allows NHibernate cache regions to
be configured with different properties. For this purpose, NCache has a
configuration file named NCacheNHibernate.xml, which contains all regions’
configurations used by NHibernate Second Level Cache. This file can be
placed at root directory of application or in [InstallDir]/config
.
Sample NCacheNHibernate.xml
<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="demoClusteredCache"
priority="Default" expiration-type="sliding" expiration-period="180" />
<region name="default" cache-name="demoClusteredCache" priority="default"
expiration-type="none" expiration-period="0" />
</cache-regions>
</application-config>
</configuration>
Multiple application configurations can be specified in NCacheNHibernate.xml.
Any NHibernate application will use one of the specified configurations based on
the application-id
specified in Web.config.
For more details on configurations and features supporting NHibernate like Configuring Cacheable Objects and Database Synchronization, refer to Using NCache as NHibernate Second Level Cache in Programmers' Guide.