所以这就是我的问题。我的Spring Boot应用程序使用Hibernate / JPA和ehCache进行二级缓存。我想用Dropwizard / Coda-Hale指标测试ehCache,但我不确定如何做到这一点。如果我手动创建缓存实例,那么它很简单。你只需使用如here所示的装饰器。但由于它是Spring / Hibernate,我无法控制缓存。有什么想法我会如何设置它?
答案 0 :(得分:5)
我相信我终于明白了。事实证明,为返回Spring类ehCacheCacheManager的cacheManager添加Spring JavaConfig仍然可以用来访问底层的ehCache类并装饰它们。以下是我如何使用它
@Bean
public EhCacheCacheManager ehCacheCacheManager() {
final EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(ehCacheManagerFactoryBean().getObject());
net.sf.ehcache.CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
String[] cacheNames = cacheManager.getCacheNames();
for (String cacheName : cacheNames) {
Cache cache = cacheManager.getCache(cacheName);
cacheManager.replaceCacheWithDecoratedCache(cache, InstrumentedEhcache.instrument(metricRegistry, cache));
}
return ehCacheCacheManager;
}
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
cacheManagerFactoryBean.setShared(true);
return cacheManagerFactoryBean;
}