redis批量插入操作
方式一:
RedisTemplate<String, Object> redisTemplate;
public void listCache(List<T> lists){
Map<String, String> map = new HashMap<>();
for(T list: lists){
map.put(list.getA(), list.getB());
}
redisTemplate.opsForValue().multiSet(map);
}
方式二:
public void listCache(List<T> lists){
JedisPool jedisPool = new JedisPool(new JedisPoolConfig());
Jedis jedis = jedisPool.getResource();
Pipeline pipeline = jedis.pipelined();
for (T list: lists){
pipeline.set(list.getA(), list.getB());
}
pipeline.sync();
jedis.close();
}
方式三:
public void listCache(List<T> lists){
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.auth("hwq");//密码
jedis.select(6);//选块
Pipeline pipeline = jedis.pipelined();
int i = 0;
for (T list: lists){
pipeline.sadd(list.getA(), list.getB());
i++;
}
pipeline.sync();
jedis.close();
}