小编Gui*_*uez的帖子

如何在不覆盖默认 spring-cache 的情况下创建辅助 CacheManager

我的 application.yml 中有一个默认的 redis 缓存配置:

cache:
    type: redis
    redis:
      time-to-live: 7200000 # 2 hour TTL - Tune this if needed later
  redis:
    host: myHost
    port: myPort
    password: myPass
    ssl: true
    cluster:
      nodes: clusterNodes
    timeout: 10000
Run Code Online (Sandbox Code Playgroud)

它工作得很好,我不想为它创建任何自定义缓存管理器。

但是,我的代码中有一些缓存不需要使用 redis。因此,我想创建第二个 CacheManager,它是一个简单的 ConcurrentHashMap 并使用 @Cacheable 指定它

为此,我创建了一个新的 CacheManager Bean:


@Configuration
@EnableCaching
@Slf4j
class CachingConfiguration {

    @Bean(name = "inMemoryCache")
    public CacheManager inMemoryCache() {
        SimpleCacheManager cache = new SimpleCacheManager();
        cache.setCaches(Arrays.asList(new ConcurrentMapCache("CACHE"));
        return cache;
    }

}
Run Code Online (Sandbox Code Playgroud)

这导致 inMemoryCache 成为我的默认缓存,并且我所有其他 @Cacheable() 都尝试使用 inMemoryCache。我不希望我创建的 CacheManager bean 成为我的默认值。无论如何,我可以指定它是次要的,并且不会阻止 spring-cache …

java spring caching redis spring-cache

5
推荐指数
1
解决办法
2328
查看次数

标签 统计

caching ×1

java ×1

redis ×1

spring ×1

spring-cache ×1