Spring Hibernate With EhCache

DZone 's Guide to

Spring Hibernate With EhCache

Learn how to implement EhCache, an open-source caching solution, to boost performance in Spring applications by offloading the database.

· Performance Zone ·
Free Resource

EhCache is an open-source, standards-based cache for boosting performance, simplifying scalability, and offloading your database. EhCache is used to improve performance by reducing the load on underlying resources.  It can also be used for RESTful server caching, application persistence, and distributed caching.

In simple words, cache means a store of things that will be required in the future and can be retrieved rapidly.

How Much Will an Application Speed Up With Caching?

It depends on a multitude of factors:

  1. At what level data present in the cache can and is reused by the application.
  2. The proportion of response time eased by caching.

Spring contains the cache manager interface org.springframework.cache.CacheManager, so we need to provide a concrete implementation for cache storage.

One of the implementations for caching is EhCache. This project involves EhCache in Spring with the Hibernate configuration. It follows the ModelView Controller (MVC) architecture.

First, have a Maven project in Eclipse using the web app archetype.

Project Structure

Maven Dependency

Add this Maven dependency into your application’s classpath to implement EhCache in your project:


Configuring the Cache Storage

package com.spring.ehcache.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@EnableCaching /*This annotation registers CacheInterceptor or AnnotationCacheAspect,
 which will detect cache annotations like @Cacheable, @CachePut, and @CacheEvict.*/
public class EHCacheConfig {

/*  It shows cache enabling with EhCache-related beans in a separate configuration class.
 *  Overriding these two beans is not needed if you want to stay with the default definition,
 *  but we wanted to make cache transactions aware to synchronize put/evict operations with
 *  ongoing Spring-managed transactions.*/

    public EhCacheManagerFactoryBean ehCacheManagerFactory() {
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        return cacheManagerFactoryBean;
    public EhCacheCacheManager ehCacheCacheManager() {
        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
        return cacheManager;


Controller Class

package com.spring.ehcache.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.spring.ehcache.entity.EhCacheEntity;
import com.spring.ehcache.model.EhCacheModel;
import com.spring.ehcache.repository.EhCacheRepositoryImpl;

public class EhCacheController {

 EhCacheRepositoryImpl myrepo;

 @RequestMapping(value = "test", method = {
 public List < EhCacheModel > myMethod() {

  List < EhCacheModel > demo = new ArrayList < > ();
  List < EhCacheEntity > myDemoEntityList = myrepo.getEntity();

  for (EhCacheEntity myDemoEntity: myDemoEntityList) {
   EhCacheModel d = new EhCacheModel();
  return demo;


DAO Implementation

package com.spring.ehcache.repository;

import java.util.List;

import org.hibernate.Criteria;
import org.springframework.cache.annotation.Cacheable;

import com.spring.ehcache.entity.EhCacheEntity;

public class EhCacheRepositoryImpl extends EhCacheRepository {

 /* @Cacheable indicates that the result of invoking a method (or all methods in a class) can be cached.
  * Each time an advised method is invoked, the caching behavior will be applied, 
  * checking whether the method was already invoked for the given arguments.*/

 @Cacheable(value = "ehcache")
 public List < EhCacheEntity > getEntity() {
  List < EhCacheEntity > myDemoEntitylist = null;
  Criteria cr = currentSession().createCriteria(EhCacheEntity.class);
  myDemoEntitylist = cr.list();
  return myDemoEntitylist;

Create a ehcache.xml file to tell EhCache how and where to cache the data.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="ehcache"
           maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"
           timeToLiveSeconds="3600" overflowToDisk="true"/>

To get all data, execute these queries in the MySQL database:

CREATE SCHEMA `ehcaching` ;

CREATE TABLE `ehcaching`.`ehcache` (
  `id` INT(11) NOT NULL,
  `name_column` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`));

INSERT INTO `ehcaching`.`ehcache` (`id`, `name_column`) VALUES ('1', 'dev');
INSERT INTO `ehcaching`.`ehcache` (`id`, `name_column`) VALUES ('2', 'ankit');
INSERT INTO `ehcaching`.`ehcache` (`id`, `name_column`) VALUES ('3', 'akshay');
INSERT INTO `ehcaching`.`ehcache` (`id`, `name_column`) VALUES ('4', 'rahul');


You can download the code here.

caching ,ehcache ,hibernate ,performance ,spring ,tutorial

Published at DZone with permission of Dev Bhatia . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}