pipline的使用可以很大程度提高redis使用性能,在性能优化工作中经常使用;
使用
使用并最好有一两个项目实践了;
private void setCache(List<String> names, String key) {
Pipeline pipeline = redis.pipelined();
for (String name : names) {
pipeline.zadd(key, name, score);
}
pipeline.expire(key, CACHE_EXPIRE_SECONDS);
pipeline.syncAndReturnAll();
}
syncAndReturnAll会返回一个01数字的list,代表成功和失败;
原理
简单来讲就是减少了网路请求时间,已队列的形式发送一组数据,同时减少了IO;
性能测试
结论:写入1w个string用时在500到750毫秒之间,写入10w个string用时在5秒到6秒之间,具体需根据自身redis配置测试了。