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
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
Next Step is to perform changes in your spring Configuration file. Below is my Spring configuration file which contains EhCache configurations
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
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
To Clear Cache we can just use TriggerRemove Annotation
Complete Source Code is present at following location
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mY2ZiYzA4NWQtNmQ0ZS00ZWM0LTlkMzktMmM3YmJmZjUzNDEy&hl=en_US
Hibernate uses different type of Caches
Put ehcache.xml at your classpath. It could be in classes or WEB-INF folder
In you Entity just add Annotations related to ehcache and thats it.
Complete Source Code is present at following location
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mMDJiMDU4MmUtZWFkYy00MTAxLWFjMmYtMWYyMGNhMWY2Mjdj&hl=en_US
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mY2ZiYzA4NWQtNmQ0ZS00ZWM0LTlkMzktMmM3YmJmZjUzNDEy&hl=en_US
Integration with Hibernate as a Second Level Cache
Hibernate uses different type of Caches
· The first cache type is the session cache. The session cache caches object within the current session.
· The second cache type is the query Cache. The query cache is responsible for caching queries and their results.
· The third cache type is the second level cache. The second level cache is responsible for caching objects across sessions.
EhCache is used as second level cache. EhCache integration with Hibernate is quite simple as well. You just need to define some properties in persistence.xml file.
EhCache jars are available via maven, simply add the following dependency to your pom.xml
Next step is to perform changes in persistence.xml
Put ehcache.xml at your classpath. It could be in classes or WEB-INF folder
In you Entity just add Annotations related to ehcache and thats it.
Complete Source Code is present at following location
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B0YFdqXJcI3mMDJiMDU4MmUtZWFkYy00MTAxLWFjMmYtMWYyMGNhMWY2Mjdj&hl=en_US
Awesome
ReplyDeleteThis is very helpful. I have one question about ehcache integration with existing spring webapp. Is terracotta *server* needed for running above code?
ReplyDeleteno its not required
ReplyDeleteHi Adeel, great blog...
ReplyDeleteIn my current task, i need to have a cluster of cache servers(2 servers, and one is back-up of another). And this cluster should be in sync always.
I went through the ehcache documentation and did small programs, but not really sure how to start on this cache cluster.
Can you please explain about this scenario.
Thanks,
-Venkat
Also very good tutorial thanks a lot.
ReplyDeletehow do you run this example?
ReplyDeleteHi 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,
ReplyDeleteException 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,
ReplyDeleteException 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.
so thank of this tutorial! Hope you have more the lession like this! Thank agains!
ReplyDeleteYou saved my life after 3 hours intensive debugging :)Pega Online Training
ReplyDeleteThanks for giving Good Example,It is very useful to us
ReplyDeleteFantastic article, Viral. Very well written, clear and concise. One of the best links explaining one to many and hierarchy Hibernate. Thanks a lot.It is uaefull to me and my training Hibernateonlinetraining center.