springboot+redis切换指定数据库or动态切换数据库
redis切换数据库项目使用指定的数据库项目中动态切换数据库项目使用指定的数据库需求1:甲方有很多系统用redis,我们这个系统需要指定用到4数据库在配置文件中设置数据库spring:redis:host: xxport: xxpassword: xxxdatabase: 1redis配置类@Configuration@EnableCaching/*** redis配置*/public class
·
redis切换数据库
项目使用指定的数据库
需求1:
甲方有很多系统用redis,我们这个系统需要指定用到4数据库
在配置文件中设置数据库
spring:
redis:
host: xx
port: xx
password: xxx
database: 1
redis配置类
@Configuration
@EnableCaching
/**
* redis配置
*/
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private Integer database;
/**
连接配置
*/
@Bean
RedisStandaloneConfiguration redisStandaloneConfiguration() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(database);
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
return redisStandaloneConfiguration;
}
/**
Lettuce 配置
*/
@Bean
@Primary
RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(redisStandaloneConfiguration(),
LettuceClientConfiguration.builder()
// 超时时间
.commandTimeout(Duration.ofMinutes(30)).build());
}
/**
* 模板
* @param connectionFactory 连接工厂
* @return {@link RedisTemplate<Object, Object>}
*/
@Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate()
{
RedisTemplate<Object, Object> template = new RedisTemplate<>();
// 这里不再使用默认的RedisConnectionFactory配置,走我们自定义的
template.setConnectionFactory(connectionFactory());
FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
serializer.setObjectMapper(mapper);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
...
项目中动态切换数据库
需求2:实现在项目中动态的切换
@Component
public class RedisService {
@Resource
public RedisTemplate redisTemplate;
@Value("${spring.redis.database}")
private Integer database;
/**
* 设置数据库索引
*
* @param dbIndex 数据库索引
*/
private void setDbIndex(Integer dbIndex) {
if (dbIndex == null || dbIndex > 15 || dbIndex < 0) {
dbIndex = database;
}
LettuceConnectionFactory redisConnectionFactory = (LettuceConnectionFactory) redisTemplate
.getConnectionFactory();
if (redisConnectionFactory == null) {
return;
}
redisConnectionFactory.setDatabase(dbIndex);
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 属性设置后
redisConnectionFactory.afterPropertiesSet();
// 重置连接
redisConnectionFactory.resetConnection();
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 设置缓存对象
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @param index 指数
*/
public <T> void setCacheObject(Integer index, final String key, final T value) {
setDbIndex(index);
redisTemplate.opsForValue().set(key, value);
}
...
测试使用
redisService.setCacheObject("xxx", "567567567567567");
redisService.setCacheObject(8, "ces", "567567567567567");
更多推荐
已为社区贡献2条内容
所有评论(0)