Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot cache No cache could be resolved for Builder

According to link, the simplest configuration to use cache in spring boot is using CacheManager (an cache Map would be initialized in this class):

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

and in serviceImpl.java:

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

But it throws:

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.

EDIT: if I assign a cacheName in cacheManager, and use it in the advised method, the exception is gone. But all methods in the bean would be cached, while I only assigned @Cacheable on one method.

like image 953
Tiina Avatar asked Jul 21 '17 08:07

Tiina


1 Answers

I am using Ehcache and i had the same issue because i had two different names for cache and 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>
like image 83
Bibhuti Avatar answered Sep 21 '22 06:09

Bibhuti