Second-level NHibernate Cache
NHibernate has a first-level or session-level cache which is used by default. Each session contains its session-level cache which is not shared with other sessions. This cache is disposed of whenever the session is closed. However, NHibernate allows to use second-level, across the session cache which spans across the session's lifetime and can be shared by multiple sessions.
Note
This feature is also available in NCache Professional.
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's second cache in existing NHibernate applications without any code change. All that is required is to change the NHibernate configuration file to plug in NCache as a cache provider.
The Second-level Cache can be configured by enabling it and configuring a Second-level Cache provider in NHibernate configurations.
Configuring NCache as Second-level Cache
Important
Use NHibernate Version 2.2.3 to use NCache as NHibernate 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
Install NCache NuGet package for application without NCache installation. Execute any of the following commands in Package Manager Console inside Visual Studio:
- For Enterprise:
Install-Package NHibernate.NCache
- For Profesional:
Install-Package NHibernate.NCache.Professional
- For Open Source:
Install-Package NHibernate.NCache.OpenSource
- For Enterprise:
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 a Second-level Cache in the application by setting the
cache.use_second_level_cache
property to True.
<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 the NCache configuration file for NHibernate (NCacheNHibernate.xml). This application-id must be specified in the application's configuration file (Web.config):
<appSettings>
<add key="ncache.application_id" value="myapp" />
</appSettings>
Configuring Cacheable Objects
Enabling the use of a Second-level Cache does not cache each class object by default. Instead, classes that need to be cached are to be marked cacheable in the class mapping (.hbm.xml) file. To mark a class cacheable, add the following tag in the class's configuration:
<cache usage="read-write" region ="AbsoluteExpirationRegion"/>
In the above tag, properties usage
and region
can be changed.
Members | Description |
---|---|
region |
Specifies the name of the Second-level Cache region to be used for this class object. If no region is specified, a Fully Qualified Name (FQN) will be used as a region name with default region configurations. |
usage |
Specifies caching concurrency strategy to be used for this class. NHibernate allows the following three concurrency strategies to be used for caching:Read-Write Nonstrict-Read-Write Read-Only |
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's Second-level Cache. This file can be placed in the root directory of the application or in %NCHOME%\config
(Windows) or /opt/ncache/config
(Linux).
<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>
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 the application's App.config/Web.config file.
The following are the configurable options in the application-config
section.
Member | Description |
---|---|
application-id |
This option lets the user specify a unique id for the current application-config . This ID is used to select the appropriate configuration for a particular application. |
enable-cache-exception |
Identifies whether the exceptions that occurred in NCache will propagate to NHibernate provider or not. |
default-region-name |
Allows to specify a default region for the application. If a particular region's configurations are not found, default region configurations will be used. Specified default region configuration must exist in the cache-regions section. |
key-case-sensitivity |
This option allows you to specify whether cache keys will be case-sensitive or not. This option has to be configured according to the database used. If a database is case-sensitive, set this option to True, otherwise, False. |
cache-regions |
This section lets the user configure multiple cache regions for the NHibernate application. Each region's configuration is specified in the region tag. |
region |
This tag contains the configurations for one particular region. The following options can be configured for any particular region.
Default Low BelowNormal Normal AboveNormal NotRemovable
|
database-dependencies |
It lets the user specify database dependencies for the current NHibernate application.
|
See Also
Using NHibernate Query Caching
Configuring Database Synchronization with NHibernate
Entity Framework Cache Provider