Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6552324
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: 数据库开发技术

2018-01-19 17:01:01


在单实例redis环境,我们可以采用Pipeline的方式写入数据,采用管道的方式将多个key值一起提交,较一个key值提交一次,减少了IO,速度会明显提升,下面是采用管道和不采用管道的方式比较执行10万次的,时间比较。

  1. package com.hxl;

  2. import java.util.LinkedHashSet;
  3. import java.util.Set;

  4. import redis.clients.jedis.BinaryJedis;
  5. import redis.clients.jedis.HostAndPort;
  6. import redis.clients.jedis.Jedis;
  7. import redis.clients.jedis.JedisCluster;
  8. import redis.clients.jedis.JedisPoolConfig;

  9. import redis.clients.jedis.Pipeline;

  10. public class PipelineTest {

  11.     /**
  12.      *
  13.      * @param args
  14.      */

  15.     public static void main(String[] args) {
  16.         int count = 100000;
  17.         long start = System.currentTimeMillis();
  18.         withoutPipeline(count);
  19.         long end = System.currentTimeMillis();
  20.         System.out.println("withoutPipeline: " + (end - start));
  21.         start = System.currentTimeMillis();
  22.         usePipeline(count);
  23.         end = System.currentTimeMillis();
  24.         System.out.println("usePipeline: " + (end - start));
  25.     }

  26.     private static void withoutPipeline(int count) {
  27.      JedisPoolConfig poolConfig = new JedisPoolConfig();
  28.      // 最大连接数
  29.      poolConfig.setMaxTotal(1);
  30.      // 最大空闲数
  31.      poolConfig.setMaxIdle(1);
  32.      // 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
  33.      // Could not get a resource from the pool
  34.      poolConfig.setMaxWaitMillis(1000);
  35.      //Set nodes = new LinkedHashSet();
  36.      //nodes.add(new HostAndPort("192.168.56.91", 6379));
  37.      //nodes.add(new HostAndPort("192.168.56.91", 7379));
  38.      //nodes.add(new HostAndPort("192.168.56.92", 6379));
  39.      //nodes.add(new HostAndPort("192.168.56.92", 7379));
  40.      //nodes.add(new HostAndPort("192.168.56.93", 6379));
  41.      //nodes.add(new HostAndPort("192.168.56.93", 7379));
  42.      //JedisCluster cluster = new JedisCluster(nodes, poolConfig);
  43.     
  44.         Jedis jr = null;
  45.         try {
  46.             jr = new Jedis("192.168.56.91", 8379);
  47.             for (int i = 0; i < count; i++) {
  48.                 jr.incr("age1");
  49.             }
  50.         } catch (Exception e) {
  51.             e.printStackTrace();
  52.         }
  53.         finally {
  54.             if (jr != null) {
  55.                 jr.disconnect();
  56.             }
  57.         }
  58.     }

  59.     private static void usePipeline(int count) {

  60.      JedisPoolConfig poolConfig = new JedisPoolConfig();
  61.      // 最大连接数
  62.      poolConfig.setMaxTotal(1);
  63.      // 最大空闲数
  64.      poolConfig.setMaxIdle(1);
  65.      // 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
  66.      // Could not get a resource from the pool
  67.      poolConfig.setMaxWaitMillis(1000);
  68.      Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
  69.      //nodes.add(new HostAndPort("192.168.56.91", 6379));
  70.      //nodes.add(new HostAndPort("192.168.56.91", 7379));
  71.      //nodes.add(new HostAndPort("192.168.56.92", 6379));
  72.      //nodes.add(new HostAndPort("192.168.56.92", 7379));
  73.      //nodes.add(new HostAndPort("192.168.56.93", 6379));
  74.      //nodes.add(new HostAndPort("192.168.56.93", 7379));
  75.      //JedisCluster cluster = new JedisCluster(nodes, poolConfig);        
  76.         
  77.         
  78.         Jedis jr = null;
  79.         try {
  80.             jr = new Jedis("192.168.56.91",8379);
  81.             
  82.             Pipeline pl = jr.pipelined();
  83.             for (int i = 0; i < count; i++) {
  84.                 pl.incr("age2");
  85.             }
  86.             pl.sync();
  87.         } catch (Exception e) {
  88.             e.printStackTrace();
  89.         }
  90.         finally {
  91.             if (jr != null) {
  92.                 jr.disconnect();
  93.             }
  94.         }
  95.     }
  96. }
采用管道的消耗时间明显少很多:

阅读(11276) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~