Memory Object Caching System in Java (J2EE)

I needed a memory-caching system in some of features, I wrote simple-caching system but it was too simple and can’t scale (distributed).

I have heard about memcached and found it’s Java port j-memached which seems to be little complex to setup?

I looked at JCS and EHCache. I chose EHCache because it was already in use somewhere in our application and I could do most of things with it:-

  • J2EE servlet caching.
  • General stuff (Programmatic APIs to cache things, even if it’s simple HashMap).

It was easy to setup EHCache. In some servlets, I was able to enable caching without touching servlet code:-

  • Write filter classes, which extends from EHCache-construct classes
  • Add cache entry and configuration in EHCache.xml
  • Add entry of filter classes in web.xml

That’s it, caching is in place for simpler things but complex things and more control, I am going to use EHCache API.

Caching is important part of any web-application, one needs to think about it while designing the system. Some people might not agree and think it as premature optimization. I believe, if you know about requirements (scalebility or expected-user-count) then there is nothing wrong to think about optimization.

BTW! Do you think there is better option than EHCache? Please let me know. Thanks.

  • I am trying to understand why do you need a java port of memcached.
    standard memcached should be able to meet most needs . a java api is available from Greg Whalin (

  • @Tarun: I am really newbie when it comes to memcached etc, I had just heard about it and saw other folks using in different environments (Apache etc). When I started looking at it, I understood it needs to run separatly (I guess ‘d’ in last makes sense to me now).
    I was working in Java, I thought of Java port that can run within our container. But you are right, we just need client-lib. On the other hand, running something as daemon requires ops/sys-admin level efforts and decisions..
    My requirement was simple so I opted something where I don’t get into loop for such decisions etc. EHCache, just need some jars, solves my problems in short term as well as long term for my features..
    In future, if we decide to go for memcached, I hope it would be minimal effort to move existing stuff to memcached since it’s simple..
    Any more ideas and tips are welcome..
    Thanks for your comment, it helps me learn more about things..