Cannot find the cache named xxx for the builder in spring boot application
Solution 1
because of you don't add
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<CaffeineCache> caffeineCaches = new ArrayList<>();
for (CacheConstant cacheType : CacheConstant.values()) {
caffeineCaches.add(new CaffeineCache(cacheType.toString(),
Caffeine.newBuilder()
.expireAfterWrite(cacheType.getExpires(), TimeUnit.SECONDS)
.maximumSize(cacheType.getMaximumSize())
.build()));
}
cacheManager.setCaches(caffeineCaches);
return cacheManager;
}
Solution 2
Alternatively:
Add ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns='http://www.ehcache.org/v3'>
<persistence directory="${java.io.tmpdir}" />
<!-- Default cache template -->
<cache-template name="default">
<expiry>
<tti unit="hours">4</tti>
<!-- <ttl unit="minutes">2</ttl> -->
</expiry>
<listeners>
<listener>
<class>com.org.lob.support.LoggingTaskCacheListener</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>REMOVED</events-to-fire-on>
<events-to-fire-on>UPDATED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap unit="MB">10</heap>
<offheap unit="MB">50</offheap>
<disk persistent="true" unit="GB">1</disk>
</resources>
<!--
<heap-store-settings>
<max-object-graph-size>2000</max-object-graph-size>
<max-object-size unit="kB">5</max-object-size>
</heap-store-settings>
-->
</cache-template>
<!-- Cache configurations -->
<cache alias="books" uses-template="default" >
<key-type>java.lang.String</key-type>
<value-type>com.org.lob.project.repository.entity.Book</value-type>
</cache>
<cache alias="files" uses-template="default" >
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
</cache>
</config>
And add the following in application.properties.
# Cache
spring.cache.jcache.config=classpath:ehcache.xml
Related videos on Youtube
Priya
Updated on June 04, 2022Comments
-
Priya about 2 years
I have a Spring boot application where I want to use spring bot cache on a repository method.I have specified @EnableCaching annotaion in my spring boot app, When I try to use @Cacheable annotation on my repository method, it throws error like
java.lang.IllegalArgumentException: Cannot find cache named 'cache' for Builder[public abstract java.util.Optional myRepoMethod(java.lang.String,java.lang.String)] caches=[cache] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false' at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:84) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:224) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.(CacheAspectSupport.java:669) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:237) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.(CacheAspectSupport.java:570) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:317) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE] at com.sun.proxy.$Proxy140.findByUserIdAndProduct(Unknown Source) ~[?:?]
I don't know where I have missed out !!
My repository method looks like ,
@Cacheable("cache") Optional<ModelClass> findByUserIdAndProduct(String userId, String product);