一、Jedis的管道Pipe操作
Jedis的管道Pipe操作可以实现高效率的redis操作,通过以下对比,我们可以发现,管道操作的效率是普通操作的8倍
package com.redis.pipe;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
/**
* \* Created with IntelliJ IDEA.
* \* Author: huyi
* \* Date: 2017/2/16 10:22
* \* Description:
* \
*/
public class JedisTest {
public static void main(String [] args){
int count = 1000;
long start = System.currentTimeMillis();
withOutPipe(count);
long end = System.currentTimeMillis();
System.out.println("withoutPipeline: " + (end-start));
start = System.currentTimeMillis();
withPipe(count);
end = System.currentTimeMillis();
System.out.println("usePipeline: " + (end-start));
}
public static void configSet(int count){
Jedis jedis = new Jedis("192.168.182.129",6379);
jedis.configSet("timeout", "1");
jedis.disconnect();
}
public static void withOutPipe(int count){
Jedis jedis = new Jedis("192.168.182.129",6379);
for(int i =0; i<count; i++){
jedis.incr("testKey1");
}
jedis.disconnect();
}
public static void withPipe(int count){
Jedis jedis = new Jedis("192.168.182.129",6379);
Pipeline pl = jedis.pipelined();
for(int i =0; i<count; i++){
pl.incr("testKey1");
}
pl.sync();
jedis.disconnect();
}
}
二、Jedis的超时设置
jedis可以设置服务器,和在服务器操作redis-cli命令一样,拿超时命令举例
package com.redis.pipe;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
/**
* \* Created with IntelliJ IDEA.
* \* Author: huyi
* \* Date: 2017/2/16 10:22
* \* Description:
* \
*/
public class JedisTest {
public static void main(String [] args){
int count = 1000;
long start = System.currentTimeMillis();
withOutPipe(count);
long end = System.currentTimeMillis();
System.out.println("withoutPipeline: " + (end-start));
start = System.currentTimeMillis();
withPipe(count);
end = System.currentTimeMillis();
System.out.println("usePipeline: " + (end-start));
}
public static void configSet(int count){
Jedis jedis = new Jedis("192.168.182.129",6379);
jedis.configSet("timeout", "1");
jedis.disconnect();
}
public static void withOutPipe(int count){
Jedis jedis = new Jedis("192.168.182.129",6379);
for(int i =0; i<count; i++){
jedis.incr("testKey1");
}
jedis.disconnect();
}
public static void withPipe(int count){
Jedis jedis = new Jedis("192.168.182.129",6379);
Pipeline pl = jedis.pipelined();
for(int i =0; i<count; i++){
pl.incr("testKey1");
}
pl.sync();
jedis.disconnect();
}
}