Spring Hibernate With EhCache

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 future, and can be retrieved rapidly.

How much will an application speed up with Caching?

It depends on the multitude of factors being:

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

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

One of the implementations for caching is : EhCache.

This project involves Ehcache in Spring with hibernate configuration. It follows Model View Controller ( MVC ) architecture.

Firstly take a maven project in eclipse using webapp archetype.

Project Structure

Maven dependency

Add this maven dependency in your application’s classpath to implement ehcache in your project :

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.5.Final</version>
</dependency>

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;

@Configuration
@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.*/

	 @Bean
	    public EhCacheManagerFactoryBean ehCacheManagerFactory() {
	        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
	        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
	        cacheManagerFactoryBean.setShared(true);
	        return cacheManagerFactoryBean;
	    }
	    @Bean
	    public EhCacheCacheManager ehCacheCacheManager() {
	        EhCacheCacheManager cacheManager = new EhCacheCacheManager();
	        cacheManager.setCacheManager(ehCacheManagerFactory().getObject());
	        cacheManager.setTransactionAware(true);
	        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;

@RestController
public class EhCacheController {
	
	@Autowired
	EhCacheRepositoryImpl myrepo;
	
	@RequestMapping(value="test" , method = { RequestMethod.OPTIONS, RequestMethod.GET })
	public List<EhCacheModel> myMethod() {
		
		List<EhCacheModel> demo = new ArrayList<>();
		List<EhCacheEntity> myDemoEntityList=myrepo.getEntity();
		
		for(EhCacheEntity myDemoEntity : myDemoEntityList) {
			EhCacheModel d=new EhCacheModel();
			d.setId(myDemoEntity.getUserId());
			d.setName(myDemoEntity.getName());
			demo.add(d);
		}
		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>
<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <cache name="ehcache"
           maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"
           timeToLiveSeconds="3600" overflowToDisk="true"/>
</ehcache>

To get all data, execute these queries in 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');

Output

You can download the code from here : Download

Leave a Reply