tags:

views:

768

answers:

2

I have 2 different webapps (package into different war files) which needs to share some data via a cache (ehcache). I want to test out this idea with you to see if it works.

My idea is to create a service that bootstraps/accesses the ehcache and package that inside a jar. Then package that jar into the two wars:

  • WAR1: ehcache-service.jar
  • WAR2: ehcache-service.jar

Would ehcache work in such a configuration ?

+1  A: 

The problem of your configuration is that each war will be loaded with separate ClassLoader and there will be two instances of your ehcache-service - each web application has it's own copy of service. You can implement your caching service as a Web-Service (for example) and use it from war1 and war2.

gedevan
+2  A: 

You need to create a separate jar(s) with all classes (and all their dependencies) which instances you plan to cache and then deploy this jar as well as ehcache.jar as a library (depending of what application server you use the procedure might be different), in case of Tomcat 6 that means just copy jars to lib folder.

What happen then is that ehcache and your domain classes will be loaded by the classloader shared by all web applications, so instances will be cached and accessible in memory.

The dependencies of your domain classes are important, so you should see whether this approach is feasible in your project. It might also affect the way you restart the web applications.


Furthermore you should be aware that cache and sharing are not necessary the same thing. Cache is an optimization. If you put object instance into cache it might be evicted immediately if, for instance, cache doesn't have enough storage space or eviction policy configuration. So may be you have to review the way you plan to use ecache in general.

Gennady Shumakher
I'm using Tomcat so libraries shared by all webapps go into CATALINA_HOME/common/lib. There is a more complicated issue since I want to replicate the cache across multiple tomcat servers in a cluster.
ashitaka
I like your statement "Cache is an optimization". I might need to revert back to using the database to store shared state because I can use the SERIALIZABLE isolation level to ensure consistent reads/writes.
ashitaka