1 回答
TA贡献2003条经验 获得超2个赞
我能够保留 yml 中的默认 Redis 配置,并使用以下代码添加一个新的 inMemory CacheManager(我使用的是 Clustered Redis):
@Bean
@Primary
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public LettuceClusterConnection getConnection(LettuceConnectionFactory lettuceConnectionFactory) {
LettuceClusterConnection clusterConnection = (LettuceClusterConnection) lettuceConnectionFactory.getClusterConnection();
return clusterConnection;
}
@Bean(name = "inMemoryCache")
public CaffeineCacheManager inMemoryCache() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCaffeine(caffeineCacheBuilder());
return cacheManager;
}
Caffeine< Object, Object > caffeineCacheBuilder() {
return Caffeine.newBuilder()
.initialCapacity(3000)
.maximumSize(40000)
.expireAfterAccess(30, TimeUnit.MINUTES);
}
添加回答
举报