我正在尝试在我的 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();
,并希望它只会在第一次被击中。但是,每次我发送请求时,它都会触及服务方法的内部。有任何想法吗?非常感谢。