前兩天琢磨了一下,也不能總用默認(rèn)的0號(hào)緩存庫,于是就上網(wǎng)查了資料,整合了動(dòng)態(tài)切換緩存庫的功能,但是還不滿足于我,畢竟我用的可是FastJson第三方庫,于是又在這基礎(chǔ)上修改成了可以配合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端口號(hào)
@Value("${spring.redis.port}")
private int port;
//redis密碼
@Value("${spring.redis.password}")
private String password;
//默認(rèn)數(shù)據(jù)庫
private int defaultDB;
//多個(gè)數(shù)據(jù)庫集合
@Value("${spring.redis.dbs}")
private List<Integer> dbList;
//RedisTemplate實(shí)例
private static Map<Integer, RedisTemplate<String, Object>> redisTemplateMap = new HashMap<>();
/**
* 初始化連接池
*/
@PostConstruct
public void initRedisTemplate() {
defaultDB = dbList.get(0);//設(shè)置默認(rèn)數(shù)據(jù)庫
for (Integer db : dbList) {
//存儲(chǔ)多個(gè)RedisTemplate實(shí)例
redisTemplateMap.put(db, redisTemplate(db));
}
}
public LettuceConnectionFactory redisConnection(int db) {
RedisStandaloneConfiguration server = new RedisStandaloneConfiguration();
server.setHostName(host); // 指定地址
server.setDatabase(db); // 指定數(shù)據(jù)庫
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;
}
/**
* 指定數(shù)據(jù)庫進(jìn)行切換
*
* @param db 數(shù)據(jù)庫索引
* @return
*/
public RedisTemplate<String, Object> getRedisTemplateByDb(int db) {
return redisTemplateMap.get(db);
}
/**
* 使用默認(rèn)數(shù)據(jù)庫
*
* @return
*/
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplateMap.get(defaultDB);
}
// // RedisTemplate模板
// public RedisTemplate<String, Object> redisTemplate(int db) {
// //為了開發(fā)方便,一般直接使用<String,Object>
// RedisTemplate<String, Object> template = new RedisTemplate<>();
// template.setConnectionFactory(redisConnection(db)); //設(shè)置連接
// //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配置文件也需要做相應(yīng)的修改,其實(shí)很簡單,只需要在原有的基礎(chǔ)上改為如下格式即可
spring
redis:
dbs: 0,1,2,3,4,5
host: localhost
port: 6379
password: redis-password
簡單記錄一下