Hibernate Caching in NCache
Hibernate offers two level caching. First-level caching module provides a session-level in-process caching. During session creation, hibernate associates a session cache with the process. Whenever an object loads from the database, it maintains a copy of the object in the session cache. If the object needs to be fetched again within the same session, a cached copy of the object is returned. For a particular transaction, if the same object is updated multiple times, it is only updated in the session cache. Only the final state of an object is persisted in the database while committing the transaction, thus, avoiding repetitive update calls. Hibernate also provides second level caching. NCache provides this support. It provides a Hibernate's second level cache provider with the added benefits of NCache distributing caching. The NCache provider can be configured as Hibernate's second level cache without any code change. To learn more about Hibernate Caching, please see Hibernate docs.
Hibernate Second Level Cache Provider
Using NCache as a second level cache for Hibernate enables applications to use NCache's many distributed caching features:
Specify Multiple Regions
Multiple Hibernate regions can be specified and configured in NCache's configuration file for Hibernate. These regions are identified using region names and can be configured with multiple NCache features. You can configure each region using NCache caching features.
NCache's Cache for Region
NCache allows you to specify NCache's cache instance for each region separately. You can configure each region on a separate NCache's 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 the cache. This priority is then used for data eviction policies.
- Absolute Expiration
NCache's configurable Absolute Expiration feature keeps data in the cache up-to-date by invalidating old data after a configured time interval.
- Sliding Expiration
You can also configure the cache region to have Sliding Expiration for items in the cache. Using Sliding Expiration, data is not being used since the configured interval will be removed automatically by NCache.
- Cache Item Priority
Configure Multiple Regions Even on Single NCache's Cache Instance
The NCache provider for Hibernate allows you to use a single NCache instance for multiple Hibernate cache regions. NCache keeps a record of data and its associated Hibernate region.
To ensure data consistency with the database, NCache provides a Database Dependency feature for Hibernate applications that can be configured on an entity basis. Database dependencies synchronize cache data with the database such that any change in the database record invalidates the respective entity in the cache. Thus it retrieves an up-to-date record even if the database is updated by an application other than the Hibernate application.
Query Caching Feature to Cache Large Queries' Results
Complex and large queries, if executed repeatedly with no result change, can put a huge load on the database. To avoid multiple executions of such queries, NCache allows you to cache such queries using Hibernate's query cache feature. So such a query and its results, executed for one Hibernate application, become available to all Hibernate applications using that database without querying the database.
No Code Change Required
NCache allows you to use all these features through configuration files with no code change necessary.