我可以使用ConcurrentMapCacheFactoryBean定制Spring Boot simple ConcurrentMapCache的存储吗?

我可以使用ConcurrentMapCacheFactoryBean定制Spring Boot simple ConcurrentMapCache的存储吗?,spring,spring-boot,caching,guava,spring-cache,Spring,Spring Boot,Caching,Guava,Spring Cache,我使用的是带有一些@Cacheable和@EnableCaching注释的自动配置。我有几个指定的缓存。缓存按预期工作 现在,我想添加TTL和一些其他定制到我的缓存。我试图用番石榴缓存替换ConcurrentMapCacheManager使用的默认ConcurrentHashMap。我遵循了来自的建议,并使用ConcurrentMapCacheFactoryBean进行了此操作: @Bean public ConcurrentMapCacheFactoryBean cacheFactoryBea

我使用的是带有一些
@Cacheable
@EnableCaching
注释的自动配置。我有几个指定的缓存。缓存按预期工作

现在,我想添加TTL和一些其他定制到我的缓存。我试图用番石榴缓存替换ConcurrentMapCacheManager使用的默认ConcurrentHashMap。我遵循了来自的建议,并使用ConcurrentMapCacheFactoryBean进行了此操作:

@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanA() {
    ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
    cacheFactoryBean.setName("A");
    cacheFactoryBean.setStore(CacheBuilder.newBuilder()
            .expireAfterWrite(DURATION, TimeUnit.SECONDS)
            .build()
            .asMap());
    return cacheFactoryBean;
}
我的缓存名称是A、B或C

@Cacheable("A")
因为我需要3个缓存,看起来我需要用不同的缓存名称配置3个ConcurrentMapCacheFactoryBean

@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanA() {
    cacheFactoryBean.setName("A");
    ...
}
@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanB() {
    cacheFactoryBean.setName("B");
    ...
}
@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanC() {
    cacheFactoryBean.setName("C");
    ...
}
这样做合适吗?有没有更简单的方法来配置ConcurrentMapCache存储?这样,我就失去了先前的自动缓存创建功能,在这里,我只需将注释
@Cacheable(“D”)
添加到某个方法中,缓存D就会自动创建


我知道SpringBoot已经用自动配置的咖啡因支持取代了Guava缓存支持,它具有TTL的属性,我也可以切换到这一轨道。但我喜欢这种方法的灵活性,也很好奇。

我最终忘记了
ConcurrentMapCacheFactoryBean
,并自定义
CacheManager
来覆盖
ConcurrentMapCacheManager
创建ConcurrentMapCache(名称)
。这很简单:

@Configuration
public class TtlConcurrentMapCacheConfiguration {

    public static final int TTL_SECONDS = 60;

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager() {
            @Override
            protected Cache createConcurrentMapCache(String name) {
                // storeByValue is false by default (for ConcurrentMapCaches) and wont be true unless we set it
                // explicitly. I think it is enough to just not support storeByValue, and keep things simple.
                // https://github.com/spring-projects/spring-framework/issues/18331
                if (isStoreByValue()) {
                    throw new IllegalStateException("storeByValue is not supported");
                }
                ConcurrentMap<Object, Object> store = CacheBuilder.newBuilder()
                        .expireAfterWrite(TTL_SECONDS, TimeUnit.SECONDS)
                        .build()
                        .asMap();
                return new ConcurrentMapCache(name, store, isAllowNullValues());
            }
        };
    }

}
@配置
公共类TtlConcurrentMapCacheConfiguration{
公共静态最终整数TTL_秒=60;
@豆子
公共缓存管理器缓存管理器(){
返回新的ConcurrentMapCacheManager(){
@凌驾
受保护的缓存CreateCourrentMapCache(字符串名称){
//storeByValue在默认情况下为false(对于ConcurrentMapCaches),除非我们设置它,否则不会为true
//我认为不支持storeByValue并保持简单就足够了。
// https://github.com/spring-projects/spring-framework/issues/18331
if(isStoreByValue()){
抛出新的IllegalStateException(“不支持storeByValue”);
}
ConcurrentMap store=CacheBuilder.newBuilder()
.expireAfterWrite(TTL_秒,时间单位为秒)
.build()
.asMap();
返回新的ConcurrentMapCache(名称、存储、isAllowNullValues());
}
};
}
}
我不知道我在哪里找到了ConcurrentMapCacheFactoryBean,也不知道为什么有三个,每个缓存一个,就像我的问题中那样工作。也许我的一些搜索发现了一些信息,导致我走错了方向