前两天琢磨了一下,也不能总用默认的0号缓存库,于是就上网查了资料,整合了动态切换缓存库的功能,但是还不满足于我,毕竟我用的可是FastJson第三方库,于是又在这基础上修改成了可以配合FastJson库的一套配置类。
RedisConfig.class
package cc.buckler.config;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created on 2022/2/23
*
* @author Buckler
* @description RedisConfig
*/
@Configuration
public class RedisConfig {
//redis地址
@Value("${spring.redis.host}")
private String host;
//redis端口号
@Value("${spring.redis.port}")
private int port;
//redis密码
@Value("${spring.redis.password}")
private String password;
//默认数据库
private int defaultDB;
//多个数据库集合
@Value("${spring.redis.dbs}")
private List<Integer> dbList;
//RedisTemplate实例
private static Map<Integer, RedisTemplate<String, Object>> redisTemplateMap = new HashMap<>();
/**
* 初始化连接池
*/
@PostConstruct
public void initRedisTemplate() {
defaultDB = dbList.get(0);//设置默认数据库
for (Integer db : dbList) {
//存储多个RedisTemplate实例
redisTemplateMap.put(db, redisTemplate(db));
}
}
public LettuceConnectionFactory redisConnection(int db) {
RedisStandaloneConfiguration server = new RedisStandaloneConfiguration();
server.setHostName(host); // 指定地址
server.setDatabase(db); // 指定数据库
server.setPort(port); //指定端口
server.setPassword(password); //指定密码
LettuceConnectionFactory factory = new LettuceConnectionFactory(server);
factory.afterPropertiesSet(); //刷新配置
return factory;
}
public RedisTemplate<String, Object> redisTemplate(int db) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnection(db));
FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(serializer);
template.setHashValueSerializer(serializer);
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnection(db));
template.afterPropertiesSet();
return template;
}
/**
* 指定数据库进行切换
*
* @param db 数据库索引
* @return
*/
public RedisTemplate<String, Object> getRedisTemplateByDb(int db) {
return redisTemplateMap.get(db);
}
/**
* 使用默认数据库
*
* @return
*/
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplateMap.get(defaultDB);
}
// // RedisTemplate模板
// public RedisTemplate<String, Object> redisTemplate(int db) {
// //为了开发方便,一般直接使用<String,Object>
// RedisTemplate<String, Object> template = new RedisTemplate<>();
// template.setConnectionFactory(redisConnection(db)); //设置连接
// //Json序列化配置
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// ObjectMapper om = new ObjectMapper();
// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// jackson2JsonRedisSerializer.setObjectMapper(om);
// //String的序列化
// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// //key采用String的序列化方式
// template.setKeySerializer(stringRedisSerializer);
// //hash的key采用String的序列化方式
// template.setHashKeySerializer(stringRedisSerializer);
// //value序列化方式采用jackson
// template.setValueSerializer(jackson2JsonRedisSerializer);
// //hash序列化方式采用jackson
// template.setHashValueSerializer(jackson2JsonRedisSerializer);
// template.afterPropertiesSet();
// return template;
// }
}
那么配置类有了,搭配的application.yml配置文件也需要做相应的修改,其实很简单,只需要在原有的基础上改为如下格式即可
spring
redis:
dbs: 0,1,2,3,4,5
host: localhost
port: 6379
password: redis-password
简单记录一下