redis
redis切换db
@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常用存取
1.list
List<User> list = redisTemplate.opsForList().range("aaa", 0, -1);
while (redisTemplate.opsForList().size("aaa") > 0){
redisTemplate.opsForList().leftPop("aaa");
}
redisTemplate.opsForList().rightPushAll("aaa", list,100);
2.对象
redisTemplate.opsForValue().set(key, value);
redis单线程为什么那么快:
- 纯内存操作
- 使用IO多路复用技术
- 非CPU密集型任务