Hibernate Second Level Cache
Hibernate's 1st level caching module provides session level in the 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 the object is maintained in the session cache. If the object is to be fetched again in the same session, the cached copy of the 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 the database thus avoiding repetitive update calls.
Hibernate Second Level Cache Provider
NCache provides Hibernate's second level cache provider. NCache provider can be configured as Hibernate's 2nd level cache without any code change. Using NCache as 2nd level cache for Hibernate enables applications to use NCache's distributed caching features.
It provides the following features with second level cache provider:
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. Each region can be configured with NCache caching features.NCache's Cache for Region: NCache allows you to specify NCache's cache instance for each region separately. Each region can be configured on 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 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 a configured time interval.
- Sliding Expiration: Cache region can also be configured to have sliding expiration for items in cache. Using sliding expiration, data which is not used since the configured interval will be removed automatically by NCache.
- 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.
Configure Multiple Regions Even on Single NCache's Cache Instance: NCache provider for Hibernate allows you to use a single NCache's instance for multiple Hibernate cache instances. NCache keeps record of data and its associated Hibernate region.
Database Dependencies: To ensure data consistency with database, NCache provides a database dependency feature for Hibernate applications that can be configured on entity bases. Database dependency synchronizes cache data with the database such that any change in the database record invalidates the respective entity in cache. Thus Hibernate retrieves an up to date record even if the database is updated by an application other than 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 that such 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 features through configuration files with no code change required.
See Also
Initialize Cache
Add/Update in Cache
Configure Cacheable Objects and Regions
Configure Hibernate Application
Use Query Caching