在Spring Boot应用程序中找不到用于构建器的名为xxx的缓存

时间:2018-08-27 10:53:08

标签: spring-boot spring-cache

我有一个Spring boot应用程序,我想在存储库方法上使用spring bot缓存。我在spring boot应用程序中指定了@EnableCaching注解,当我尝试在存储库方法上使用@Cacheable注解时,它将引发错误喜欢

  

java.lang.IllegalArgumentException:找不到名为“缓存”的缓存   用于Builder [public abstract java.util.Optional   myRepoMethod(java.lang.String,java.lang.String)] caches = [缓存] |   键=''| keyGenerator =''| | cacheManager =''| | cacheResolver =''|   条件=''|除非=''|同步='假'在   org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:84)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:224)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.cache.interceptor.CacheAspectSupport $ CacheOperationContext。(CacheAspectSupport.java:669)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:237)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.cache.interceptor.CacheAspectSupport $ CacheOperationContexts。(CacheAspectSupport.java:570)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:317)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)   〜[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)   〜[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)   〜[spring-aop-5.0.6.RELEASE.jar:5.0.6.RELEASE]在   com.sun.proxy。$ Proxy140.findByUserIdAndProduct(未知来源)〜[?:?]

我不知道我错过了什么地方!

我的存储库方法看起来像

@Cacheable("cache")
Optional<ModelClass> findByUserIdAndProduct(String userId, String product);

2 个答案:

答案 0 :(得分:1)

因为您没有添加

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

答案 1 :(得分:0)

就我而言,我想使用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注释一起放置。