前言
spring-data-redis對reids基本操作進行很好的封裝,操作時候簡單便捷,但是spring-data-redis在初始化選redis某個庫后不能進行修改,如果想以數(shù)據(jù)庫區(qū)分業(yè)務邏輯需要進行選庫操作
配置
- 為了簡單配置,在springboot的基礎(chǔ)上配置reids操作
在springboot配置的基礎(chǔ)上引入spring-data-redis,不同版本的springboot引入的spring-boot-starter-data-redis可能不同,具體參看官方文檔。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
在application.yml或者application.properties中配置基本的連接信息如下:
spring:
redis:
database: 0
host: 127.0.0.1
password:
pool:
max-active: 8
max-wait: 5000
max-idle: 8
min-idle: 3
timeout: 5000
之后自定義RedisTemplate繼承spring-data-redis的StringRedisTemplate,定義REDIS_DB_INDEX為ThreadLocal類型,每個線程選庫操作互不影響。
public static ThreadLocal<Integer> REDIS_DB_INDEX = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 0;
}
};
@Override
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
try {
Integer dbIndex = REDIS_DB_INDEX.get();
//如果設置了dbIndex
if (dbIndex != null) {
if (connection instanceof JedisConnection) {
if (((JedisConnection) connection).getNativeConnection().getDB().intValue() != dbIndex) {
connection.select(dbIndex);
}
} else {
connection.select(dbIndex);
}
} else {
connection.select(0);
}
} finally {
REDIS_DB_INDEX.remove();
}
return super.preProcessConnection(connection, existingConnection);
}
}
注冊自定義的RedisTemplate到Bean容器中
@Configuration
public class RedisConfig {
@Bean
public RedisSerializer fastJson2JsonRedisSerializer() {
return new FastJson2JsonRedisSerializer<Object>(Object.class);
}
@Bean
public RedisTemplate initRedisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer fastJson2JsonRedisSerializer) throws Exception {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
上述代碼使用FastJson座位redis默認序列化工具,配置FastJson序列化需要自定義序列化器
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJson2JsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
之后便可以進行選庫操作:
RedisTemplate.REDIS_DB_INDEX.set(10);
redisTemplate.opsForValue().set("author","stevejobson");
當然最好自己寫一份RedisServcie封裝上述操作。
注意:
Redis進行選庫操作并不能提高運行效率,不過如果業(yè)務量過大,可以將不同模塊的不同業(yè)務的reids存儲存放到不同庫中,有效的區(qū)分數(shù)據(jù)
當然強烈建議在設計redis key的時候進行能的對業(yè)務進行區(qū)分,便于后期維護