在单实例redis环境,我们可以采用Pipeline的方式写入数据,采用管道的方式将多个key值一起提交,较一个key值提交一次,减少了IO,速度会明显提升,下面是采用管道和不采用管道的方式比较执行10万次的,时间比较。
-
package com.hxl;
-
-
import java.util.LinkedHashSet;
-
import java.util.Set;
-
-
import redis.clients.jedis.BinaryJedis;
-
import redis.clients.jedis.HostAndPort;
-
import redis.clients.jedis.Jedis;
-
import redis.clients.jedis.JedisCluster;
-
import redis.clients.jedis.JedisPoolConfig;
-
-
import redis.clients.jedis.Pipeline;
-
-
public class PipelineTest {
-
-
/**
-
*
-
* @param args
-
*/
-
-
public static void main(String[] args) {
-
int count = 100000;
-
long start = System.currentTimeMillis();
-
withoutPipeline(count);
-
long end = System.currentTimeMillis();
-
System.out.println("withoutPipeline: " + (end - start));
-
start = System.currentTimeMillis();
-
usePipeline(count);
-
end = System.currentTimeMillis();
-
System.out.println("usePipeline: " + (end - start));
-
}
-
-
private static void withoutPipeline(int count) {
-
JedisPoolConfig poolConfig = new JedisPoolConfig();
-
// 最大连接数
-
poolConfig.setMaxTotal(1);
-
// 最大空闲数
-
poolConfig.setMaxIdle(1);
-
// 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
-
// Could not get a resource from the pool
-
poolConfig.setMaxWaitMillis(1000);
-
//Set nodes = new LinkedHashSet();
-
//nodes.add(new HostAndPort("192.168.56.91", 6379));
-
//nodes.add(new HostAndPort("192.168.56.91", 7379));
-
//nodes.add(new HostAndPort("192.168.56.92", 6379));
-
//nodes.add(new HostAndPort("192.168.56.92", 7379));
-
//nodes.add(new HostAndPort("192.168.56.93", 6379));
-
//nodes.add(new HostAndPort("192.168.56.93", 7379));
-
//JedisCluster cluster = new JedisCluster(nodes, poolConfig);
-
-
Jedis jr = null;
-
try {
-
jr = new Jedis("192.168.56.91", 8379);
-
for (int i = 0; i < count; i++) {
-
jr.incr("age1");
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
finally {
-
if (jr != null) {
-
jr.disconnect();
-
}
-
}
-
}
-
-
private static void usePipeline(int count) {
-
-
JedisPoolConfig poolConfig = new JedisPoolConfig();
-
// 最大连接数
-
poolConfig.setMaxTotal(1);
-
// 最大空闲数
-
poolConfig.setMaxIdle(1);
-
// 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
-
// Could not get a resource from the pool
-
poolConfig.setMaxWaitMillis(1000);
-
Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
-
//nodes.add(new HostAndPort("192.168.56.91", 6379));
-
//nodes.add(new HostAndPort("192.168.56.91", 7379));
-
//nodes.add(new HostAndPort("192.168.56.92", 6379));
-
//nodes.add(new HostAndPort("192.168.56.92", 7379));
-
//nodes.add(new HostAndPort("192.168.56.93", 6379));
-
//nodes.add(new HostAndPort("192.168.56.93", 7379));
-
//JedisCluster cluster = new JedisCluster(nodes, poolConfig);
-
-
-
Jedis jr = null;
-
try {
-
jr = new Jedis("192.168.56.91",8379);
-
-
Pipeline pl = jr.pipelined();
-
for (int i = 0; i < count; i++) {
-
pl.incr("age2");
-
}
-
pl.sync();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
finally {
-
if (jr != null) {
-
jr.disconnect();
-
}
-
}
-
}
-
}
采用管道的消耗时间明显少很多:
阅读(11355) | 评论(0) | 转发(0) |