redis使用Pipeline管道提升写入速度

上传人:m**** 文档编号:69220619 上传时间:2019-01-13 格式:DOCX 页数:3 大小:24.22KB
返回 下载 相关 举报
redis使用Pipeline管道提升写入速度_第1页
第1页 / 共3页
redis使用Pipeline管道提升写入速度_第2页
第2页 / 共3页
redis使用Pipeline管道提升写入速度_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《redis使用Pipeline管道提升写入速度》由会员分享,可在线阅读,更多相关《redis使用Pipeline管道提升写入速度(3页珍藏版)》请在金锄头文库上搜索。

1、redis使用Pipeline管道提升写入速度在单实例redis环境,我们可以采用Pipeline的方式写入数据,采用管道的方式将多个key值一起提交,较一个key值提交一次,减少了IO,速度会明显提升,下面是采用管道和不采用管道的方式比较执行10万次的,时间比较。1. packagecom.hxl;2.3. importjava.util.LinkedHashSet;4. importjava.util.Set;5.6. importredis.clients.jedis.BinaryJedis;7. importredis.clients.jedis.HostAndPort;8. impo

2、rtredis.clients.jedis.Jedis;9. importredis.clients.jedis.JedisCluster;10. importredis.clients.jedis.JedisPoolConfig;11.12. importredis.clients.jedis.Pipeline;13.14. publicclassPipelineTest15.16. /*17. *18. * param args19. */20.21. publicstaticvoidmain(Stringargs)22. intcount=100000;23. longstart=Sys

3、tem.currentTimeMillis();24. withoutPipeline(count);25. longend=System.currentTimeMillis();26. System.out.println(withoutPipeline: +(end-start);27. start=System.currentTimeMillis();28. usePipeline(count);29. end=System.currentTimeMillis();30. System.out.println(usePipeline: +(end-start);31. 32.33. pr

4、ivatestaticvoidwithoutPipeline(intcount)34. JedisPoolConfig poolConfig=newJedisPoolConfig();35. / 最大连接数36. poolConfig.setMaxTotal(1);37. / 最大空闲数38. poolConfig.setMaxIdle(1);39. / 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:40. / Could not get a resource from the pool41. poolConfig.setMaxWaitMillis(

5、1000);42. /Setnodes = new LinkedHashSet();43. /nodes.add(new HostAndPort(192.168.56.91, 6379);44. /nodes.add(new HostAndPort(192.168.56.91, 7379);45. /nodes.add(new HostAndPort(192.168.56.92, 6379);46. /nodes.add(new HostAndPort(192.168.56.92, 7379);47. /nodes.add(new HostAndPort(192.168.56.93, 6379

6、);48. /nodes.add(new HostAndPort(192.168.56.93, 7379);49. /JedisCluster cluster = new JedisCluster(nodes, poolConfig);50. 51. Jedis jr=null;52. try53. jr=newJedis(192.168.56.91,8379);54. for(inti=0;icount;i+)55. jr.incr(age1);56. 57. catch(Exceptione)58. e.printStackTrace();59. 60. finally61. if(jr!

7、=null)62. jr.disconnect();63. 64. 65. 66.67. privatestaticvoidusePipeline(intcount)68.69. JedisPoolConfig poolConfig=newJedisPoolConfig();70. / 最大连接数 71. poolConfig.setMaxTotal(1);72. / 最大空闲数73. poolConfig.setMaxIdle(1);74. / 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:www.f-1.cc75. / Could not get

8、 a resource from the pool76. poolConfig.setMaxWaitMillis(1000);77. Setnodes=newLinkedHashSet();78. /nodes.add(new HostAndPort(192.168.56.91, 6379);79. /nodes.add(new HostAndPort(192.168.56.91, 7379);80. /nodes.add(new HostAndPort(192.168.56.92, 6379);81. /nodes.add(new HostAndPort(192.168.56.92, 737

9、9);82. /nodes.add(new HostAndPort(192.168.56.93, 6379);83. /nodes.add(new HostAndPort(192.168.56.93, 7379);84. /JedisCluster cluster = new JedisCluster(nodes, poolConfig);85. 86. 87. Jedis jr=null;88. try89. jr=newJedis(192.168.56.91,8379);90. 91. Pipeline pl=jr.pipelined();92. for(inti=0;icount;i+)93. pl.incr(age2);94. 95. pl.sync();96. catch(Exceptione)97. e.printStackTrace();98. 99. finally100. if(jr!=null)101. jr.disconnect();102. 103. 104. 105. 采用管道的消耗时间明显少很多:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 数据库

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号