Tuesday, October 4, 2011

EhCache Integration with Spring and Hibernate. Step by Step Tutorial

EhCache is a very popular open source caching framework widely used for Java based Caching.

Currently it’s owned by Terracotta.org and 2.5 beta releases is available for download.

In this article I just focused on EhCache Integration with Spring and Hibernate also I just explained few basic concepts which are required to understand Code. For detailed Caching Concepts Please visit http://ehcache.org/

Cache Manager, Caches and Elements are main entities of EhCache

EhCache consists of a CacheManager, which manages Caches. Caches contain Elements, which are essentially name value pairs. Caches are physically implemented either in-memory, or on disk.

Integration with Spring

EhCache integration with Spring is quite simple. You just need to define some properties in spring configuration xml file and its ready to use. Spring is using Annotation to integrate EhCache and by this we can add caching to any method results

EhCache Annotations for Spring is available via maven, simply add the following dependency to your pom.xml

Important point to note here is that Cache Manager Name and its Configuration Location. We can use cacheManager in our code to store and retrieve information from Cache.

ehcache.xml file is configuration file for EhCache where we can define configuration details of EhCache. In my case here is content of my ehcache.xml file

Here it’s clear that we have created a cache with name Customer, I will use this Cache to store Customer details

Next step is to use it in your code. In my example I created a simple Spring based application where we have Customer Controller which is used to retrieve Customer Information

We have Customer Object with Customer Id, name and address details and related setter getter methods

Next step is to create Controller. Customer Controller is very simple. I am using Spring Restful Web services to retrieve Customer Information. In my Controller I have no logic related to EhCache. I just have two versions of getCustomer method (one with Annotation and one without Annotation), save Customer and Clear Cache method. Code is simple and self explanatory for any Spring MVC developer.

If you notice Controller is calling Customer Service. Customer Service is our main class and it’s used to get Customer Information. It can get information from Database using Hibernate or from any backend. Here I just provided basic implementation. Idea is to understand usage of EhCache

Following Service methods are used to retrieve and store information in Cache

Previously we have defined “cacheManager” object in our Spring.xml file and also we have defined a Cache with name “customer” in ehcache.xml file. Its time to use both these configurations

getCache method is use to retrieve Cache Object and we are storing new Element in this cache. Element requires key, value pair so I am using id filed as a key. Later on we can use same key to retrieve Customer Object from Cache.

Another great way of using EhCache is Spring Annotation

Add Cacheable Annotation to methods you would like to cache. In our case we are using it for getCustomer method. If we call getCustomer method with customerId 1, first time complete method will be called. If we will call this method again with same customerId then response will be returned from Cache.

Hi Adeel, how would I able to run your Hibernate annotation example? It seems that i can't locate the persistence.xml file because I got this error, Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named annotation at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) at com.annotation.TestAnnotation.main(TestAnnotation.java:11)

i.e. when I run as TestAnnotation.java as a java application in Eclipse. Please help.

Hi Adeel, how would I able to run your Hibernate annotation example? It seems that i can't locate the persistence.xml file because I got this error, Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named annotation at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) at com.annotation.TestAnnotation.main(TestAnnotation.java:11)

i.e. when I run as TestAnnotation.java as a java application in Eclipse. Please help.