How to disable ehcache in spring

930 views
Skip to first unread message

Mohit

unread,
Sep 3, 2011, 9:33:13 AM9/3/11
to Ehcache Spring Annotations
I need a quick help from you to fix a small problem.

In one of my project (using spring as core container), i am using
ehcache to cache data. I am using spring ehcache annotations project
(http://code.google.com/p/ehcache-spring-annotations/) for the same.

I want to have flexibility to enable and disable ehcache based on a
external property. I read ehcache documentation and found that it
reads system property net.sf.ehcache.disabled internally and if it set
to true cache will be disabled and they recommend to pass this as -
Dnet.sf.ehcache.disabled=true in the command line

I wanted to control it through externalized spring property file.

Then i thought of setting this system property in my spring
application context file using MethodInvokingFactoryBean based on a
externalized property.

Here is the code

<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System"/
>
<property name="targetMethod" value="getProperties"/>
</bean>
</property>
<property name="targetMethod" value="putAll"/>
<property name="arguments">
<util:properties>
<prop key="net.sf.ehcache.disabled">"$
{ehcache.disabled}"</prop>
</util:properties>
</property>
</bean>

Obviously ehcache.disabled is controlled via my externalized property
file.

Plumbing works great but i guess order is not coming into place. By
the time Application context is setting system property, cache is
initialized and when cache was being initialized there was no property
net.sf.ehcache.disabled, hence cache is not getting disabled. When i
run application in debug mode and try to get
System.getProperty("net.sf.ehcache.disabled") after application
context is initialized, it is giving me the right value. (I tried both
true and false).

One more thing i want to mentioned i am using spring wrapper to
initialize my cache.(code below)

<ehcache:annotation-driven self-populating-cache-scope="method"/>

<ehcache:config cache-manager="cacheManager">
<ehcache:evict-expired-elements interval="20"/>
</ehcache:config>

<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"/>

I believe that disabling cache can not be that hard.

What is it that i am missing? Is there any easy way to do it in spring
except command line -D?

Hope my question is clear.

Waiting for your analysis and answer.

Eric Dalquist

unread,
Sep 3, 2011, 10:49:41 PM9/3/11
to ehcache-sprin...@googlegroups.com
There isn't much ehcache-spring-annotations can do for you here. All the library does is take a pre-configured cache manager and use it to get caches to use for annotation configured proxies.

I'd check on the ehcache email list and see if someone there has a suggestion for a run-time toggle to enable and disable caching.

-Eric
Reply all
Reply to author
Forward
0 new messages