com.github.bootfastconfig.cache.redis.RedisConfig Maven / Gradle / Ivy
package com.github.bootfastconfig.cache.redis;
import com.github.bootfastconfig.cache.AnnotationConfigCacheBuilder;
import com.github.bootfastconfig.cache.CacheBeanNameConstant;
import com.github.bootfastconfig.cache.CacheBuilder;
import com.github.bootfastconfig.cache.CacheSimpleConfig;
import com.github.bootfastconfig.springtool.SpringBeanUtil;
import com.github.bootfastconfig.springtool.StringUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* The type Redis config.
*/
@Configuration
@ConditionalOnClass(value = {
RedisSerializer.class,
RedisCacheConfiguration.class,
RedisTemplate.class})
@ConditionalOnBean({RedisConnectionFactory.class})
public class RedisConfig implements InitializingBean {
@Autowired(required = false)
private RedisSerializer redisSerializer;
@Autowired(required = false)
private RedisCacheConfiguration redisCacheConfiguration;
@Autowired(required = false)
private List cacheConfigs;
@Autowired
private SpringBeanUtil springBeanUtil;
@Autowired(required = false)
private AnnotationConfigCacheBuilder annotationConfigCacheBuilder;
@Override
public void afterPropertiesSet() throws Exception {
if (this.redisCacheConfiguration == null) {
this.redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
}
if (this.redisSerializer != null) {
this.redisCacheConfiguration = redisCacheConfiguration();
}
}
/**
* Cache manager cache manager.
*
* @param connectionFactory the connection factory
* @return the cache manager
*/
@Bean(CacheBeanNameConstant.REDIS_CACHE_MANAGER)
@ConditionalOnMissingBean(name = {CacheBeanNameConstant.REDIS_CACHE_MANAGER})
public RedisCacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfig redisCacheConfig = initRedisCacheConfig();
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(new MyRedisCacheWriter(connectionFactory));
Collection> cacheBuilder = redisCacheConfig.getCacheBuilder();
if (!cacheBuilder.isEmpty()) {
Map collect = cacheBuilder.stream().collect(Collectors.toMap(CacheBuilder::getName, CacheBuilder::get));
builder.initialCacheNames(collect.keySet());
builder.withInitialCacheConfigurations(collect);
}
builder.cacheDefaults(redisCacheConfig.getDefaultCache());
return builder.build();
}
public RedisCacheConfiguration redisCacheConfiguration() {
//设置CacheManager的值序列化方式为 fastJsonRedisSerializer,但其实RedisCacheConfiguration默认使用StringRedisSerializer序列化key,
RedisSerializationContext.SerializationPair