Setting up caching

Let's go back to our caching settings in the hibernate.cfg.xml file:

<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

Pay attention to the first line - it contains the name of the caching engine class. Hibernate does not actually implement caching internally, instead it provides a set of interfaces with which you can implement your own caching engine.

Popular implementations include:

  • EHCache
  • OSCache
  • SwarmCache
  • JBoss TreeCache

EHCache

It can be cached in memory or on disk, as well as in cluster caching, and also supports an optional cache of Hibernate query results.

OSCache

Supports memory and disk caching in the same JVM with a rich set of expiration policies and query cache support.

SwarmCache

Cluster cache based on JGroups. It uses clustered invalidation but does not support the Hibernate query cache.

JBoss Cache

A fully transactional replicated clustered cache, also based on the JGroups multicast library. It supports replication or invalidation, synchronous or asynchronous communication, and optimistic and pessimistic locking. Hibernate query cache is supported.

These engines do not support all parallel access strategies. The actual situation is described in the table below:

Strategy/Provider Read-only non-strict read-write read-write transactional
EHCache X X X
OSCache X X X
SwarmCache X X
JBoss Cache X X

EHCache configuration example

The EHCache engine has its own configuration file called ehcache.xml. The approximate content of such a file:

<diskStore path="java.io.tmpdir"/>

<defaultCache
    maxElementsInMemory = "1000"
    eternal = "false"
    timeToIdleSeconds = "120"
    timeToLiveSeconds = "120"
    overflowToDisk = "true"
/>

<cache name = "Employee"
    maxElementsInMemory = "500"
    eternal = "true"
    timeToIdleSeconds = "0"
    timeToLiveSeconds = "0"
    overflowToDisk = "false"
/>

Here you can configure caching settings both in general and for each entity separately. Read more in the official documentation .

Analysis of caching statistics

You may also need to check how well the caching mechanism is configured. Hibernate provides a special Statistics object specifically for this.

Example:

Statistics statistics = session.getSessionFactory().getStatistics();
CacheRegionStatistics cacheStatistics = statistics.getDomainRegionStatistics(“com.codegym.employee”);

long hitCount = cacheStatistics.getHitCount();
long missCount = cacheStatistics.getMissCount();
double hitRatio = (double) hitCount / (hitCount + missCount);

We won't go into detail about this, because it will be many more years before you run out of standard caching solutions and need to improve them manually. By the way, this comment may have been extracted from a similar case:

// Dear programmer:
// When you're done "optimizing" this routine
// and realize how big of a mistake it was to do this,
// please increment the counter at the bottom as a warning
// for the next guy:
// number_of_hours_spent_here = 42
undefined
1
Task
Module 4. Working with databases, level 14, lesson 7
Locked
task1407
task1407