Jedis 批量插入数据

测试环境:

测试:在 set 中插入 28629151 条 长度为 5 的字符串。

直接插入

import redis.clients.jedis.Jedis;

public class GeneratorMain {
    private static final char[] CODE = "23456789ABCDEFGHJKMNPQRSTUVWXYZ".toCharArray();
    private static final int len = CODE.length;
    public static void main(String[] args) {
        char[] val = new char[5];
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("密码");
        long start = System.currentTimeMillis();
        for (int i = 0; i < len; i++) {
            val[0] = CODE[i];
            for (int j = 0; j < len; j++) {
                val[1] = CODE[j];
                for (int k = 0; k < len; k++) {
                    val[2] = CODE[k];
                    for (int l = 0; l < len; l++) {
                        val[3] = CODE[l];
                        for (int m = 0; m < len; m++) {
                            val[4] = CODE[m];
                            jedis.sadd("code", new String(val));
                        }
                    }
                }
            }
        }//fori
        long end = System.currentTimeMillis();
        System.out.println("耗时 " + (end - start) / 1000 + "秒!");
        jedis.close();
    }
}

耗时 1101秒!

使用 Pipeline 插入

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.io.IOException;

public class Main {
    private static final char[] CODE = "23456789ABCDEFGHJKMNPQRSTUVWXYZ".toCharArray();
    private static final int len = CODE.length;
    public static void main(String[] args) {
        char[] val = new char[5];
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println(jedis.auth("密码"));
        long start = System.currentTimeMillis();
        Pipeline pipelined = jedis.pipelined();
        for (int i = 0; i < len; i++) {
            val[0] = CODE[i];
            for (int j = 0; j < len; j++) {
                val[1] = CODE[j];
                for (int k = 0; k < len; k++) {
                    val[2] = CODE[k];
                    for (int l = 0; l < len; l++) {
                        val[3] = CODE[l];
                        for (int m = 0; m < len; m++) {
                            val[4] = CODE[m];
                            pipelined.sadd("code", new String(val));
                        }
                    }
                }
            }
        }
        try {
            pipelined.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 28629151
        System.out.println(jedis.scard("code"));
        jedis.close();
        long end = System.currentTimeMillis();
        System.out.println("耗时 " + (end - start) / 1000 + "秒!");
    }
}

耗时 86秒!

使用 redis-cli –pipe

time cat ~/redis.txt | redis-cli -a 密码 --pipe

All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 28629151
cat ~/redis.txt  0.05s user 0.31s system 0% cpu 1:42.58 total
redis-cli -a lgh123 --pipe  3.51s user 1.03s system 4% cpu 1:42.71 total

Redis 是一种基于客户端/服务端模型以及请求/响应协议的 TCP 服务。这意味着通常情况下一个请求会遵循以下步骤:

  • 客户端向服务端发送一个查询请求,并监听 Socket 返回,通常是以阻塞模式,等待服务端响应。
  • 服务端处理命令,并将结果返回给客户端。

Redis 管道技术可以在服务端未响应时,客户端可以继续向服务端发送请求,并最终一次性读取所有服务端的响应。

参考:
https://www.cnblogs.com/leodaxin/p/7514760.html

你可能感兴趣的:(JAVA)