spring-boot - 无法在 spring boot 应用程序中为构建器找到名为 xxx 的缓存

标签 spring-boot spring-cache

我有一个 Spring Boot 应用程序,我想在存储库方法上使用 spring bot 缓存。我在 Spring Boot 应用程序中指定了 @EnableCaching annotaion,当我尝试在我的存储库方法上使用 @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;
}

关于spring-boot - 无法在 spring boot 应用程序中为构建器找到名为 xxx 的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037708/

相关文章:

java - 是否可以在 Spring 启动时将Web套接字与rest api一起使用?

spring-boot - 如何使用 swagger-ui(springfox-swagger2 和 springfox-swagger-ui)和 spring-boot 应用程序上传多个文件?

java - Spring Cache 与 JPA 和规范

spring-cache - Spring Cache with Redis - 如何在与 Redis 连接失败的情况下优雅地处理甚至跳过缓存

java - 如何在 Spring Boot 中将 TTL 附加到 Redis 缓存中的每个条目

java - 在 spring-boot 中为 Graphite 千分尺事件添加前缀

java - Spring Security 中的 AuthenticationSuccessHandler

java - 在专用 tomcat 上将 spring-boot 应用程序部署为 war 文件不起作用

mockito - 在 Spring 数据存储库上测试 Spring 声明式缓存支持的问题

java - Spring Cache 从值中获取键