2

I am trying to apply ehcache on my spring boot application. Here is my code.

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 under resources folder

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

My cache config is

@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;
    }
}

On my service file, I have

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

        return ...;
    }

I put the debug point on SQLQueryFactory queryFactory = getQueryFactory(); and expect it will only be hit the first time. However, everytime when I send a request, it hits the inside of the service method. Any ideas? Thanks a lot.

Laodao
  • 1,547
  • 3
  • 17
  • 39

1 Answers1

0

my bad. I didn't use autowired for my service class but used a new instance. Sorry for the misleading.

Laodao
  • 1,547
  • 3
  • 17
  • 39