【问题标题】:can we use multiple types of cache(redis & guava) in spring boot at the same time?我们可以在 Spring Boot 中同时使用多种类型的缓存(redis 和 guava)吗?
【发布时间】:2016-04-09 16:13:51
【问题描述】:

我试图在我的 Spring Boot 应用程序中为不同类型的对象提供两种缓存实现。我想根据该方法将某些对象存储在 redis 缓存中,而将其他对象存储在 guava 缓存中。每当我实现两个扩展 CachingConfigurerSupport 的类时,我都会收到一个错误,即该类只允许一个实例。谁能建议我如何做到这一点?

【问题讨论】:

  • 这已经回答here
  • 所以我需要注释 CacheConfigurer 的实现之一而不是另一个?你能举个例子解释一下吗?
  • 只需为默认实现CachingConfigurer 并在您的配置中注册一个CacheManager 类型的额外bean。

标签: caching spring-boot redis-cache google-guava-cache


【解决方案1】:

你可以像这样使用 CompositeCacheManager,

CompositeCacheManager compositeCacheManager = new CompositeCacheManager(
                ehCacheCacheManager(),
                redisCacheManager());

使用缓存时可以指定cacheManage。

@Cacheable(value = "firstCache", key = "#word", cacheManager = "ehCacheCacheManager")

或者

@Caching(
            cacheable = {
                    @Cacheable(value = "firstCache", key = "#word", cacheManager = "ehCacheCacheManager"),
                    @Cacheable(value = "extendInfo", key = "#word", cacheManager = "redisCacheManager")
            }
    )

但我不知道如何更新缓存级联。 希望有帮助。

【讨论】:

    【解决方案2】:

    blog 之后展示了不错的实现。

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 2020-06-04
      • 2017-08-29
      • 2020-10-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多