redis使用pipeline批量插入hash数据

本文通过实测展示了Redis使用pipeline批量插入数据时的性能优势,对比单次操作,批量操作性能提升显著。

import org.apache.commons.lang.math.RandomUtils;

 

import redis.clients.jedis.Jedis;

import redis.clients.jedis.Pipeline;

 

public class RedisEasyTest {

 

private static Jedis jedis = new Jedis("xx.xx.xx.xx");

 

private static Pipeline p = jedis.pipelined();

 

private static int KEY_COUNT = 10000;

 

private static int FIELD_COUNT = 10;

 

public void single() {

for (int i = 0; i < KEY_COUNT; i++) {

String key = RandomUtils.nextInt(5) + "";

for (int j = 0; j < FIELD_COUNT; j++) {

jedis.hset(key, j + "", i + j + "");

jedis.expire(key, 3600);

}

}

}

 

public void batch() {

int index = 0;

for (int i = 0; i < KEY_COUNT; i++) {

String key = RandomUtils.nextInt(5) + "";

for (int j = 0; j < FIELD_COUNT; j++) {

p.hset(key, j + "", i + j + "");

p.expire(key, 3600);

}

if (++index % 1000 == 0) {

p.sync();

}

}

p.sync();

}

 

public static void main(String[] args) {

long start = System.currentTimeMillis();

RedisEasyTest r = new RedisEasyTest();

r.single();

System.out.printf("single use %d sec \n", (System.currentTimeMillis() - start) / 1000);

start = System.currentTimeMillis();

r.batch();

System.out.printf("batch use %d sec \n", (System.currentTimeMillis() - start) / 1000);

 

}

}

输出结果:

single use 30 sec 

batch use 0 sec

 

可以看到通过pipeline批量插入数据性能是非常不错的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值