Hibernate Cache Expiration (original) (raw)

Last Updated : 20 Apr, 2026

Cache expiration in Hibernate is a mechanism used to remove outdated data from the cache to ensure data consistency and accuracy. It helps maintain a balance between performance optimization and data freshness in applications.

Types of Cache Expiration in Hibernate

Hibernate offers several cache expiration mechanisms. These mechanisms manage how and when to invalidate or delete data in the cache. Common cache expiration mechanisms include:

  1. **Time-based expiration: This type of cache expiration is based on a time interval. The cached data is considered valid only for a specified time interval, after which it is automatically removed from the cache. Hibernate provides configuration options to set the expiration time for different types of cache.
  2. **Query-based expiration: This type of cache expiration is based on specific queries. When a query is executed, the result set is cached. The cached data is invalidated when the data in the database table on which the query is based changes.
  3. **Transaction-based expiration: This type of cache expiration is based on transactions. When a transaction is committed, the cached data associated with that transaction is invalidated. This ensures that the cached data is not used in subsequent transactions.
  4. **Manual expiration: This type of cache expiration is based on manual intervention. In this case, the cached data is removed from the cache manually by the developer or administrator. This can be useful when the cached data becomes invalid due to external factors, such as changes in the application configuration or database schema.

Configuration Options for Cache Expiration

Hibernate provides multiple configuration options to control how cache expiration works. These settings can be applied either in configuration files or programmatically.

1. Configure Cache Region

A cache region is a logical grouping of cached data with its own expiration rules.

2. Set Expiration Time (TTL)

Expiration time defines how long cached data remains valid.

**Example property (used in configuration):

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

3. Choose Cache Mode

Cache mode defines how Hibernate interacts with the cache.

4. Select Cache Provider

A cache provider handles actual caching and expiration logic.

**Common providers:

Tools and Techniques for Monitoring Cache Expiration in Hibernate

Monitoring cache expiration ensures correct data and optimal performance.

Example

Java `

public class MyClass {

// Define a static cache instance
private static Cache myCache = new MyCache();

// Define a method that retrieves
// data from the cache
public MyObject getData(Long id) {

    // Attempt to retrieve data from the cache
    MyObject myObject = myCache.get(id);

    // If the data is not in the cache,
    // retrieve it from the database
    if (myObject == null) {
        myObject = retrieveDataFromDatabase(id);

        // Add the data to the cache with 
        // an expiration time of 10 minutes
        myCache.put(id, myObject, 600000);
    }

    return myObject;
}

// Define a method that retrieves data from the database
private MyObject retrieveDataFromDatabase(Long id) {
// Retrieve data from the database using Hibernate

// ... } }

`

Best Practices for Cache Expiration