Ehcache with Spring boot is not working in Test env

妖精的绣舞 提交于 2021-01-28 04:41:12

问题


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).

Code is as below:

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 {

Under 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> 

Cache Config file

@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) Is it Due to this line

factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));

Cache is not being hit /used in any other environment except Dev env?

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?

Thank you


回答1:


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).

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.



来源:https://stackoverflow.com/questions/44018771/ehcache-with-spring-boot-is-not-working-in-test-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!