2
votes

I'm trying to disable a distributed ehcache at runtime. This is needed to facilitate rolling deploys to our several servers so new version of code wouldn't interfere with servers awaiting update to the newer code.

I came up with the following class

@Component
@ManagedResource(objectName = "net.sf.ehcache:category=CustomCacheManager,name=cache-ms", description = "Custom managing of ehcache")
public class CacheJmxManagerService {

  @Autowired
  private CacheManager cacheManager;

  @ManagedOperation
  public void enableCaches(){
    String[] cacheNames = cacheManager.getCacheNames();
    for (String cacheName : cacheNames) {
      cacheManager.getCache(cacheName).setDisabled(false);
    }
  }

  @ManagedOperation
  public void disableCaches(){
    String[] cacheNames = cacheManager.getCacheNames();
    for (String cacheName : cacheNames) {
      cacheManager.getCache(cacheName).setDisabled(true);
    }
  }
}

  <bean id="cacheManager" 
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation">
          <value>${sslsConfPath}ehcache-#{applicationName}.xml</value>
        </property>
        <property name="shared" value="true"/>
      </bean>

The bean is how the ehcache manager is configured in the application context.

However when I execute them by jmx and run a jmeter test to test the load, the results are the same. Our calls are just as fast with or without the cache. Is this the right way to disable ehcache in Spring ?

1
ehcache provides its own JMX-exposable beans. Also, the @Transactional annotation is a bit puzzling as you do no database operations.beerbajay
ah right, that's a leftover of something I was trying earlier, I haven't taken it of the method yet I'll edit my post. The disabling cache method I haven't noticed on the EHcache exposed beans maybe I missed it.kenny
I checked my JMX ehcache MBeans... there's no "disable" function that I can find. You could probably simulate this by setting all of the MaxEntries attributes to 0, but actually exposing the disable functionality would be better.beerbajay

1 Answers

1
votes

Assuming everything works, what you did looks fine with regards to disabling caches.

As for the impact of having caches or not on your application, this is outside of the scope of this question.