Nhibernate Caching
NHibernate is an open source Object-Relational Mapping solution which provides a framework for mapping relational database on object oriented representation of data. This reduces significant developer's efforts for retrieving data from database. Furthermore, to improve applications performance and reduce load on database, NHibernate provides feature of caching to reduce repetitive database calls.
NHibernate First Level Cache
NHibernate First level caching module provides session level in process caching. Whenever a session is created, a session cache is associated with it. When an object is loaded from the database, a copy of object is maintained in the session cache. If the object is to be fetched again in the same session, cached copy of object is returned. In a particular transaction, if the same object is updated multiple times, it is only updated in session cache. At committing transaction, only the final state of object is persisted in database, thus avoiding repetitive update calls.
NCache as NHibernate Second Level Cache
NCache provides NHibernate second level cache provider. NCache provider can be configured as NHibernate second level cache without any code change. Using NCache as second level cache for NHibernate enable 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 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 instance for each region separately. Each region can be configured on separate NCache cache instance or one instance can also be used for multiple regions.
Cache item priority: Data in each region can be configured to have a priority in cache. This priority is then used for data eviction policies.
Absolute expiration: NCache's configurable absolute expiration feature keeps data in cache up to date by expiring old data after configured time interval.
Sliding expiration: Cache region can also be configured to have an absolute expiration for items in cache. Using sliding expiration data not 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 single NCache instance for multiple NHibernate cache instances. NCache keeps the record of data and its associated NHibernate region.
- Database Dependencies
To ensure data consistency with database, NCache provides database dependency feature for NHibernate applications that can be configured on entity bases. Database dependency synchronizes cache data with the database such that any change in database record invalidates respective entity in cache. Thus NHibernate retrieves up to date record even if database is updated by an application other than NHibernate application.
- Query Caching Feature to Cache Large Queries' Results
Complex and large queries if executed repeatedly with no result changed can put a huge load on database. To avoid multiple times execution of such queries NCache allows to cache such queries using NHibernate's query cache feature. So that such query and its results executed for one NHibernate application become available to all NHibernate applications using that database without querying the database.
- No Code Change Required
NCache allows using all features through configuration files with no code change required.
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.