У меня есть приложение для загрузки Spring, в котором я хочу использовать кеш весенних ботов в методе репозитория. Я указал аннотацию @EnableCaching в моем приложении для весенней загрузки. Когда я пытаюсь использовать аннотацию @Cacheable в моем методе репозитория, она выдает ошибку, например
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) ~[?:?]
Я не знаю, где я пропустил !!
Мой метод репозитория выглядит так:
@Cacheable("cache")
Optional<ModelClass> findByUserIdAndProduct(String userId, String product);




из-за того, что ты не добавляешь
@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;
}
В моем случае я хочу использовать redis в качестве бэкэнда кеш-хранилища.
Я получил ту же ошибку и решил ее со следующей конфигурацией:
application.yml:
spring:
redis:
database: 0
host: localhost
password:
port: 6379
cache:
type: redis
redis:
key-prefix: 'api::'
# value in milliseconds
time-to-live: 12000000
Не забудьте поместить аннотацию @EnableCaching вместе с аннотацией @SpringBootApplication.
Этот stackoverflow.com/questions/28020245/… может вам помочь