@Component
public class RedisDBChangeUtil {
@Autowired
private StringRedisTemplate redisTemplate;
public void setDataBase(int num) {
LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
if (connectionFactory != null && num != connectionFactory.getDatabase()) {
connectionFactory.setDatabase(num);
connectionFactory.afterPropertiesSet();
this.redisTemplate.setConnectionFactory(connectionFactory);
connectionFactory.resetConnection();
}
}
}
redis单线程为什么那么快:
- 纯内存操作
- 使用IO多路复用技术
- 非CPU密集型任务
redis切换db