Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use multiple ehcache.xml (in different projects, same war)?

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?

like image 700
Ivaylo Slavov Avatar asked Jan 12 '12 12:01

Ivaylo Slavov


People also ask

What is the use of Ehcache xml?

xml is included in the Ehcache distribution and can also be downloaded from http://ehcache.org/ehcache.xml. It contains comments to help you configure each element. Note that some elements documented in the sample Ehcache XML file pertain only to BigMemory and are not valid for the open-source version of Ehcache.

Is Ehcache persistent?

Ehcache offers persistence using the local disk as a cache storage tier. While Ehcache offers various disk usage choices, as of version 2.6, the recommended option for persistence is the Fast Restart store, which is available in BigMemory Go and BigMemory Max.

Where does Ehcache store data?

Data stores supported by Ehcache include: On-Heap Store - Utilizes Java's on-heap RAM memory to store cache entries. This tier utilizes the same heap memory as your Java application, all of which must be scanned by the JVM garbage collector.

Is Ehcache thread safe?

It is inherently not thread-safe to modify the value. It is safer to retrieve a value, delete the cache element, and then reinsert the value.


2 Answers

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
like image 195
Vadim Gulyakin Avatar answered Sep 27 '22 20:09

Vadim Gulyakin


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">
like image 39
MariuszS Avatar answered Sep 27 '22 18:09

MariuszS