虽然我猜这很不可能,但有没有办法在不重启服务器的情况下清除ehcache缓存? 我需要清除缓存进行一些测试 - 我不能更改代码,也不能多次重启服务器。
PS:我正在使用apache-tomcat-5.5.25 请告诉我。 谢谢, psvm
虽然我猜这很不可能,但有没有办法在不重启服务器的情况下清除ehcache缓存? 我需要清除缓存进行一些测试 - 我不能更改代码,也不能多次重启服务器。
PS:我正在使用apache-tomcat-5.5.25 请告诉我。 谢谢, psvm
您是否通过JMX公开了Ehcache?那么,您可以使用像jvisualvm这样的工具通过JMX操作清除缓存。查找提供clearAll()
操作的MBeans,例如net.sf.ehcache.CacheManager
。
使用Spring+Hibernate并暴露MBean:
import org.hibernate.Cache;
import org.hibernate.SessionFactory;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("CacheManagerMBean")
public class CacheManagerMBean {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CacheManagerMBean.class);
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
public void clearCache() {
Cache cache = sessionFactory.getCache();
if (null != cache) {
logger.info("Clearing cache...");
cache.evictAll();
cache.evictAllRegions();
logger.info("Clearing cache...Done!");
} else {
logger.error("No second level cache available for session-factory");
}
}
}
XML配置:
<bean id="jmxExporterCacheManagerMBean" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="CacheManager:type=SecondLevelCacheManager">
<ref bean="CacheManagerMBean"/>
</entry>
</map>
</property>
</bean>
然后使用jconsole连接到Java进程,并使用Mbean方法调用清除二级缓存!