caching - Spring引导缓存无法为Builder解析缓存

标签 caching spring-boot

根据 link ,在 spring boot 中使用缓存的最简单配置是使用 CacheManager (一个缓存 Map 将在这个类中初始化):

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

并在 serviceImpl.java :
 @Cacheable(cacheManager="guavaCacheManager")
 @Override
 public List<RoleVO> getDataForCreateNewOperator() {
  ...
 }

但它抛出:
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.

编辑:
如果我在 cacheManager 中分配一个 cacheName,并在建议的方法中使用它,异常就会消失。但是bean中的所有方法都会被缓存,而我只分配了@Cacheable在一种方法上。

最佳答案

我正在使用 Ehcache,我遇到了同样的问题,因为我有两个不同的
缓存和可缓存的名称。

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>

关于caching - Spring引导缓存无法为Builder解析缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232644/

相关文章:

ruby-on-rails - 如何使用 config.cache_store = :redis_store? 按环境命名缓存

javascript - HTML 脚本标记中此查询组件的用途是什么?

java - junit 测试用例通过,但仍在控制台 java 中打印异常

spring cloud gateway异常索引错误的权限中存在非法字符

java - Eclipse 链接共享与 protected 缓存

caching - 在 x64 上再次读取之前在未缓存的地址写入完整的缓存行

javascript - js中同步管理缓存

java - Spring REST api OAuth2 验证来自外部授权服务器的 token

java - Spring Boot JSF 打包为 JAR

java - 在 Linux 上的 Spring Boot 中使用 JNA 加载 .so 文件时出现问题