NHibernate Caching
NHibernate is an open-source object-relational mapping solution that provides a framework for mapping relational databases on the object-oriented representation of data - significantly reducing developer efforts in retrieving data from the database. Furthermore, to improve application performance and reduce the load on the database, NHibernate provides caching to reduce repetitive database calls.
NHibernate First Level Cache
NHibernate's first-level caching module provides session-level in-process caching. Upon session creation, it associates with a session cache. Further, when an object loads from the database, the session cache maintains an object copy. If the same session fetches the object again, it receives a cached copy. In a particular transaction, if the same object is updated multiple times, it is only updated in the session cache. Upon committing the transaction, only the final state of an object is persisted in the database, thus avoiding repetitive update calls.
NCache as NHibernate Second Level Cache
NCache provides NHibernate second-level cache provider. The NCache provider configures as an NHibernate second-level cache without any code change. Using NCache as a second-level cache for NHibernate enables the application to use NCache's distributed caching features.
NCache provides the following features with second level cache provider:
Specify Multiple Regions
Multiple NHibernate regions can be specified and configured in the NCache configuration file for NHibernate. These regions are identified using region names and can be configured with multiple NCache features.
Each region can be configured with NCache caching features.
NCache Cache for Region
NCache allows specifying NCache cache instances for each region separately. Each region configures on separate NCache cache instances, or one cache instance is used for multiple locations if necessary.
Cache item priority: Data in each region is configured to have a priority in the cache. This priority is then used for data eviction policies.
Absolute expiration: Data in each region is configured to have a priority in the cache. This priority is then used for data eviction policies. NCache's configurable Absolute Expiration feature keeps data in the cache up-to-date by expiring old data after the configured time interval.
Sliding expiration: The cache region can also be configured to have an Absolute Expiration for items in the cache. Using Sliding Expiration, data that has not been used since the configured interval will be removed automatically by NCache.
Configure Multiple Regions even on a Single NCache Cache Instance
NCache provider for NHibernate allows using a single NCache instance for multiple NHibernate cache instances. NCache keeps a record of data and its associated NHibernate region.
Database Dependencies
To ensure data consistency with the database, NCache provides a Database Dependency feature for NHibernate applications that can be configured on an entity basis. The Database Dependency synchronizes the cache data with the database such that any change in the database record invalidates the respective entity in the cache. Thus, NHibernate retrieves up-to-date records even if the database update comes from an application other than the NHibernate application.
Query Caching Feature to Cache Large Queries' Results
Complex and large queries, if executed repeatedly with no result changed, can stress the database. To avoid multiple times execution of such queries, NCache allows caching such queries using NHibernate's query cache feature. So, such a query and its results are executed for one NHibernate application and become available to all NHibernate applications using that database without querying it.
No Code Change Required
NCache allows the use of all features through its configuration files with no code changes necessary.
In This Section
Using NCache as NHibernate Second Level Cache
Explains how to configure and use NCache as NHibernate second-level cache.
Query Caching with Nhibernate
Explains how to cache queries in NCache using NHibernate.
Synchronize Database with NHIbernate second level cache
Explains how to synchronize databases with NHibernate in NCache.