I've recently began using EHCache for caching purposes. I know, how to use it in Java code, but I'm still not sure about the configuration file.
So, I have an ear project, that includes several war modules. If all these modules use ehcache, should I put a copy of ehcache.xml in WEB-INF for each module, or put it somewhere in ear file itself (META-INF maybe?)
Also, it's not clear from the documentation, whether or not all these modules will use the same cache instance? The application is deployed at glassfish, will EHCache run the same cache for each module in ear, or each module will get his own singleton instance when used like this:
CacheManager singleton = CacheManager.create();
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/context-config.xml</param-value> </context-param>
then you may add in that context-config.xml file:
<import resource="context-config.xml"/>
That context-config.xml file may contain description of cacheManager bean, that you will be able to Autowire where do you need it. So the context-config.xml may look like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
</beans>
Answering your second question. From my example, it will depend on how you will define this bean, you can define it as a singleton and then you will have only one instance of cacheManager or you can leave the instantiation to Spring. In my opinion, you can autowire cacheManager to the class with general logic for cache invalidation or cache "creation" and that will make a lot of sense. Hope that I helped you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With