19

I have a services project and a web project. I need to have eh-cache in both projects.

The idea is that if the service project is updated, it's cache-related changes (like keys and invalidation rules) will also be available, while no changes are made to the web project. Being so independent, the service project can also be used with another projects without them even knowing of eh-cache.

At this point, my web project also uses eh-cache for its own purposes. I am not much experienced with eh-cache and I fear that the two projects might clash when deployed together. I also did not find relevant information on eh-cache site.

Can you provide me some information how to best configure the two projects, so that I can achieve the above requirements?


Edit:

I am using Spring, therefore I will prefer to use it for my cache managers.

I am using the following in the context.xml for each jar with ehcache, for instance for jar 1 I have:

<ehcache:annotation-driven cache-manager="ehCacheManager1" />

<bean id="ehCacheManager1" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache-1.xml" />
</bean>

and for jar 2 I have

<ehcache:annotation-driven cache-manager="ehCacheManager2" />

<bean id="ehCacheManager2" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache-2.xml" />
</bean>

So, will both caches be up and working? I fear the ehcache:annotation-driven will get overridden by the last read context and only one cache will be operational. Am I wrong, or missing something?

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
  • Did you ever get this working? The answer on [Using Spring cache annotation in multiple modules](http://stackoverflow.com/q/8658789/16487) (and my own testing), seem to indicates it doesn't. – C. Ross Aug 07 '12 at 21:16
  • @C.Ross, actually I had to merge the ehcache xmls into one and use the merged file. We changed out caching provider to hazelcast soon enough so there was no need to work with ehcache anymore. Sorry if not being helpful – Ivaylo Slavov Aug 08 '12 at 07:58
  • 1
    Just confused. I generally assume the accepted answer *works*, which it doesn't here. – C. Ross Aug 08 '12 at 12:08

3 Answers3

3

The configurationResourceName property is used to specify the location of the ehcache configuration file.The resource is searched for in the root of the classpath. It is used to support multiple CacheManagers in the same VM.

net.sf.ehcache.configurationResourceName=/name_of_ehcache.xml
Vadim Gulyakin
  • 1,415
  • 9
  • 7
  • OK, but I want to have more than one configuration files. Will this work for my case? I mean - one config for the services-, and another for the web-project. – Ivaylo Slavov Jan 12 '12 at 12:34
  • 1
    When you create CasheManager you can pass configuration file name to constructor CacheManager manager = new CacheManager("src/config/ehcache.xml"); – Vadim Gulyakin Jan 12 '12 at 12:42
  • I clarified my question because I was having some difficulties. I'd be thankful if you respond – Ivaylo Slavov Feb 18 '12 at 16:43
  • I just tested this, and the problem is not creating two cache managers, that works just fine. The problem is that Spring doesn't know which methods to look up in which manager! – C. Ross Aug 07 '12 at 21:17
2

Try naming both cacheManagers differently in ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    name="ehCacheManager1">


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    name="ehCacheManager2">
MariuszS
  • 30,646
  • 12
  • 114
  • 155
1

if you use different cache for different WAR and you want to prevent collisions, go to echcache.xml of each WAR and define different multicastGroupPort values under cacheManagerPeerProviderFactory element. also disable distribution.

Good luck!

aviad
  • 8,229
  • 9
  • 50
  • 98