【问题标题】:ehcache 2.x + spring boot not workingehcache 2.x + spring boot 不工作
【发布时间】:2018-08-29 20:51:03
【问题描述】:

我正在尝试在我的 Spring Boot 应用程序上应用 ehcache。这是我的代码。

pom.xml

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>

资源文件夹下的ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true"
         monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="java.io.tmpdir" />

    <cache name="genericCache"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="3600" timeToLiveSeconds="86400"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

我的缓存配置是

@Configuration
@EnableCaching
@ComponentScan({ "com.overseavoice.webservices.das.*" })
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
        cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cmfb.setShared(true);
        return cmfb;
    }
}

在我的服务文件中,我有

    @Override
    @Cacheable(value="genericCache", key="'dataColl'")
    protected FetchResponse getColl(long skip, long limit)
    {
        SQLQueryFactory queryFactory = getQueryFactory();
        ...

        return ...;
    }

我将调试点放在SQLQueryFactory queryFactory = getQueryFactory(); 上,并希望它只会在第一次被击中。但是,每次我发送请求时,它都会触及服务方法的内部。有任何想法吗?非常感谢。

【问题讨论】:

    标签: spring-boot ehcache ehcache-2


    【解决方案1】:

    我的错。我没有为我的服务类使用自动装配,而是使用了一个新实例。很抱歉造成误导。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 2014-01-19
      • 1970-01-01
      • 2019-02-04
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多