Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Sensu is an open source monitoring event pipeline. Try it today.

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:

<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 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');

Output









You can download the code here.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
hibernate ,ehcache ,performance ,tutorial ,caching ,spring

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}