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.