如何在spring cache java中配置多个缓存管理器 [英] How to have multiple cache manager configuration in spring cache java

查看:710
本文介绍了如何在spring cache java中配置多个缓存管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的Web应用程序中配置多个Spring缓存管理器,并且我可以在项目的不同位置使用不同的缓存管理器。有没有办法做到这一点。

I want to have multiple spring cache managers configured in my web-application and I would be able to use different cache manager at various places in my project. Is there any way to do this.

推荐答案

有几种方法可以做到这一点,正确的答案取决于你的用法缓存。

There are several ways you can do this and the right answer depends on your usage of the cache.

如果你使用CacheManager A 90%你的用例和B为10%我建议为A创建一个默认的 CacheManager (你需要通过 CacheConfigurerSupport <指定它/ code>扩展名),类似于:

If you use CacheManager A for 90% of your use case and B for 10% I'd advise to create a default CacheManager for A (you'll need to specify it via a CacheConfigurerSupport extension), something like:

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

    @Override
    @Bean // not strictly necessary
    public CacheManager cacheManager() { ... CacheManager A }

    @Bean
    public CacheManager bCacheManager() { ... CacheManager B }
}

然后对于10%的用例,在需要使用其他缓存管理器的类的顶部添加 CacheConfig

Then for the 10% use case you add a CacheConfig at the top of the classes that need to use the other cache manager

@CacheConfig(cacheManager="bCacheManager")
public class MyService { /*...*/ }

如果只需要将一个方法用于其他缓存管理器,则可以在方法级别指定

If you need to use the other cache manager for only one method, you can specify that at method level as well

@Cacheable(cacheNames = "books", cacheManager = "bCacheManager")
public Book findById(long id) { /*...*/ }



更精细的分辨率



如果您不是这种情况,则需要一种方法来了解需要根据具体情况使用哪个缓存管理器。您可以根据目标类型( MyService )或缓存名称( books )来执行此操作。您需要实现一个 CacheResolver ,为您完成该翻译。

More fine grained resolution

If you're not in this situation, you need a way to know which cache manager needs to be used on a case-by-case basis. You can do that based on the target type (MyService) or the name of the cache (books). You'll need to implement a CacheResolver that does that translation for you.

@Configuration
@EnablleCaching
public class CacheConfig extends CachingConfigurerSupport {

    @Override
    public CacheResolver cacheResolver() { ... }
}

查看 CacheResolver 的javadoc以获取更多详细信息。在实现中,您可能有几个 CacheManager 实例(无论是否为bean),您将根据您的逻辑在内部调用以确定应使用哪个管理器。

Check the javadoc of CacheResolver for more details. In the implementation, you may have several CacheManager instances (either as bean or not) that you'll call internally based on your logic to determine which manager should be used.

我在评论中看到你指的是模块。缓存实际上是一个基础架构问题所以我强烈建议您在应用程序级别上移动该决策。您可以将缓存标记为本地,将其他标记为群集。但你应该对名称有一些命名,以使其更容易。不要在模块级别选择缓存管理器。

I saw in a comment that you were referring to "module". Caching is really an infrastructure matter so I'd strongly advice you to move that decision at the application level. You may tag cache as being "local" and others being "clustered". But you should probably have some kind of nomenclature for the name to make it easier. Don't choose a cache manager at the module level.

这篇博客文章通过其他示例说明了这一点。

this blog post illustrates this with other examples.

这篇关于如何在spring cache java中配置多个缓存管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆