ehcache and spring

October 23, 2008 at 4:28 pm 4 comments

As used by Hibernate, ehcache is a great little caching implementation which ought to be part of every Java coders arsenal. Although its complexities may seem overwhelming, it’s very easy to use as a simple caching solution.

In my situation, I needed to prevent db hits when validating codes (ie seeing if they already exist in the db). This is how easy it is to set up.


<bean id="myDao" class="myDaoImpl" init-method="setupCache">
  <property name="cacheManager" ref="cacheManager">
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="configLocation" value="classpath:ehcache.xml" />


  <diskStore path=""/>
  <cache name="myCacheName"

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class {
  private CacheManager cacheManager;
  private Cache myCache = null;

  public void setCacheManager(CacheManager cacheManager) {
    this.cacheManager = cacheManager;

  private void setupCache() {
    String cacheName = "myCacheName";
    logger.debug("Fetching cache [" + cacheName + "]");
    myCache = cacheManager.getCache(cacheName);

ehcache.xml needs to be put alongside applicationContext.xml (especially in the case of jar packaging). If you’re packaging a war, you can place it anywhere on the classpath and don’t need to specify the configLocation (it’s the default config name).

To use the cache, do something like this:

Element cachedObject = myCache.get(key);
if (cachedObject != null) {
  return cachedObject;
} else {
  fetch object from db
  // add to cache
  myCache.put(new Element(key, object))
  return object;

And that's it!

Now you’ll only hit the db when needed. Of course, it’s worth reading the reference material to see the many ways you can configure the cache. My example is purely for a 1000 object permanent memory cache, but TTL and overflow-to-disk (for example) is just as easy to set up.


Entry filed under: Development. Tags: , , , , , .

Maven dependency graph in NetBeans Converting Ant to Maven in NetBeans

4 Comments Add your own

  • 1. Sam  |  April 17, 2010 at 7:51 pm

    Thanks this is exactly what I needed.

  • 2. Halim  |  August 17, 2010 at 10:19 am

    Tnx 🙂 Simple, no bullshit explanation. Nice work!

  • 3. msns3ka  |  October 1, 2012 at 6:51 am

    Awesome Article! Simple and straight forward! I was able to configure multiple cache for our requirement 🙂

  • 4. Fredericka  |  December 23, 2012 at 11:35 am

    First off I would like to say excellent blog! I had a quick question in which I’d like to ask if you do not mind. I was interested to know how you center yourself and clear your mind before writing. I have had a hard time clearing my mind in getting my thoughts out. I do take pleasure in writing however it just seems like the first 10 to 15 minutes are generally wasted just trying to figure out how to begin. Any suggestions or hints? Cheers!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

%d bloggers like this: