Reputation: 4797
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.
Upvotes: 19
Views: 33042
Reputation: 131
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>
Upvotes: 8
Reputation: 9606
Had the same issue, Fixed by giving a proper name for cache
@Cacheable(value = "testCache")
some times, @Cacheable
is not enough.
Upvotes: 5