Hibernate Cache
Hibernate offers two caching levels. The first-level caching module provides 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 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 documentation.
Second Level Hibernate 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 cache. These regions are identified using region names and can be configured with multiple NCache features.You can configure each region in Hibernate cache 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.NCache offers several powerful caching configurations and features for better control and management of cache data across different regions. Some of these key features include:- 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.Database Dependencies
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 invalidates the respective entity in the cache. Thus it keeps 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 without changes in the result, can significantly increase the database load. To prevent unnecessary executions, NCache allows you to cache these queries using Hibernate’s query cache feature. This ensures that once a query is executed and cached by one Hibernate application, its results are available to all other Hibernate applications using the same database, eliminating the need for repeated database access.No Code Change Required
NCache enables you to leverage all its features through simple configuration, eliminating the need for any code changes.
See Also
Initialize Cache
Configure Cacheable Objects and Regions
Configure Hibernate Application
Use Query Caching