Spring启动缓存无法为Builder解析缓存

时间:2017-07-21 08:22:05

标签: caching spring-boot

根据link,在spring boot中使用缓存的最简单配置是使用CacheManager(缓存Map将在此类中初始化):

@Configuration
@EnableCaching
public class CacheService extends CachingConfigurerSupport {
    @Bean
    public CacheManager concurrentMapCacheManager() {
        ConcurrentMapCacheManager cmcm = new ConcurrentMapCacheManager();
        return cmcm;
    }

    @Bean
    @Primary
    public CacheManager guavaCacheManager() {
        GuavaCacheManager gcm = new GuavaCacheManager();
        return gcm;
    }
}

serviceImpl.java

 @Cacheable(cacheManager="guavaCacheManager")
 @Override
 public List<RoleVO> getDataForCreateNewOperator() {
  ...
 }

但它抛出:

java.lang.IllegalStateException: No cache could be resolved for 'Builder[public java.util.List getDataForCreateNewOperator()] caches=[] | key='' | keyGenerator='' | cacheManager='guavaCacheManager' | cacheResolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@38466d10'. At least one cache should be provided per cache operation.

编辑: 如果我在cacheManager中分配一个cacheName,并在建议的方法中使用它,那么异常就消失了。但是bean中的所有方法都会被缓存,而我只在一个方法上分配@Cacheable

1 个答案:

答案 0 :(得分:3)

我正在使用Ehcache,我有同样的问题,因为我有两个不同     缓存和Cacheable的名称。

Please make you use same name for cache and Cacheable.

@Cacheable("codetable")

<cache name="codetable"
        maxEntriesLocalHeap="100"
        maxEntriesLocalDisk="1000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU"
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>