问题描述
我正在使用Spring boot(1.4.2.RELEASE)和Ehcache(2.4.3)
I am using Spring boot (1.4.2.RELEASE) and Ehcache (2.4.3)
缓存已在开发环境中使用,但未在其他环境(测试和生产)中使用(命中).
Cache is being used in dev environment but it is not being used(hit) in other environments(test and prod).
代码如下:
pom.xml
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
在主类上,添加了以下用于缓存的注释
On Main class, have added below annotation for caching
@EnableCaching
public class Application {
在 src/main/resources,ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<cache name="languageCache"
maxEntriesLocalHeap="20"
overflowToDisk="false"
eternal="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>
<cache name="countryCache"
maxEntriesLocalHeap="280"
overflowToDisk="false"
eternal="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>
..
..
more entries
</ehcache>
缓存配置文件
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager getEhCacheManager() {
(new EhCacheCacheManager(getEhCacheFactory().getObject())).getCache("languageCache");
return new EhCacheCacheManager(getEhCacheFactory().getObject());
}
@Bean
public EhCacheManagerFactoryBean getEhCacheFactory() {
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
return factoryBean;
}
}
关于上述代码的几个问题:
Few questions on above code:
1)是否由于此行
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
除了Dev env以外,没有在其他任何环境中使用缓存吗?
Cache is not being hit /used in any other environment except Dev env?
2)我们是否完全需要CacheConfig文件?还是Spring Boot将使用Main Class上的注解(@EnableCaching)检测Ehcache?
2) Do we need CacheConfig file at all? or Spring boot will detect Ehcache using annotation(@EnableCaching) on Main Class?
有什么建议,为什么在其他环境中没有选择缓存(我缺少某些配置?)?
Any suggestion , why cache is not being picked up (some configuration I am missing ?) in other envs?
谢谢
推荐答案
除非您的类路径中有许多 ehcache.xml
,否则它应该可以工作.除非您在类路径中具有符合JSR107的实现(例如,Ehcache 3),否则 @EnableCaching
不会神奇地起作用.
Unless you have many ehcache.xml
in your classpath, it should work. @EnableCaching
won't work by magic unless you have a JSR107 compliant implementation in your classpath (e.g. Ehcache 3).
您的代码有效.唯一奇怪的部分是您自己调用了 getObject()
.仍然可以,但是我会做的.
Your code works. The only weird part is that you are calling the getObject()
yourself. It still works but I would have done.
@Bean
public CacheManager cacheManager(net.sf.ehcache.CacheManager cacheManager) {
return new EhCacheCacheManager(cacheManager);
}
@Bean
public EhCacheManagerFactoryBean cacheManagerFactory() {
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
return factoryBean;
}
也就是说,我实际上会做一些简单的事情:
That said, I would have, in fact, done something simpler:
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(new net.sf.ehcache.CacheManager());
}
}
此外,请注意,您确实确实需要共享缓存管理器,这确实非常罕见.它已经在应用程序上下文中共享.因此,很难将其作为单例共享.
Also, note that it is really really rare that you really need a shared cache manager. It is shared across the application context already. So it is quite rare (and frequently dangerous) to share it as a singleton.
这篇关于使用Spring Boot的Ehcache在Test Env中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!