rodert single row learning redis advanced [silver 1]

Posted by Stanley90 on Thu, 25 Jun 2020 11:19:57 +0200

redis silver I

To make a digression, recently, the epidemic situation in the capital of China is very serious. We are all in the current of the times. This is not for individuals. I hope you can protect yourself and have fun every day.



Statement: refer to the Internet, and leave a message if there is any dispute. Standing on the shoulders of our predecessors, we can see further.

Recommended reading:

rodert single row learning redis introduction [black iron]
rodert single row learning redis advanced [bronze]

1.Redis client

1.1.Redis Desktop Manager

With the tool of weighing hands, you can do half the work. It's natural to use Redis cli. I recommend a Redis visualization tool I often use, Redis Desktop Manager.

The startup interface is as follows:

As for the installation mode, it is one key installation.

Download address on official website:

Students and learners can reply to official account: 666.

2.Redis connection pool

2.2.0. Connection pool

Pool technology is widely used in system development, such as JDBC connection pool, thread pool and so on. The connection pool is a technique for creating and managing a buffer pool of connections that are ready to be used by any thread that needs them.

When dealing with a task, most of us need to finish it in milliseconds. If we repeatedly create and close resources, it will take a long time and a lot of system resources.

  • Use connection pool benefits
  1. Reduce connection creation time

The connection is created when the system is initialized, and is directly taken from the pool when necessary, which reduces the time cost.

  1. Simplified programming mode

When using connection pooling, each individual thread can operate as if it had created its own JDBC connection.

  1. Controlled resource use

The connection pool can control the resource utilization rate of a module, which will not lead to the system crash.

2.1.Redis connection pool

2.1.1. Introduction of foreword

The connection pool chapter of Redis cultivation. The introduction chapter of Redis was mentioned earlier: rodert single row learning redis introduction [black iron],rodert single row learning redis advanced [bronze] , explains the operation of Redis basic data types.

In the past, when there was no open source connection pool, many people wrote their own connection pool tool, which is simply to create a collection, store a batch of connections, and maintain them dynamically. Ensure that each connection is valid.

2.1.2.Redis connection pool

Some of the code involved in this tutorial is written in the Java language.

maven dependency, importing pom.xml file

  • pom.xml
public final class RedisUtil {
    //IP address
    private static String ADDR = "";
    //Port number
    private static int PORT = 6379;
    //redis server password
    private static String PWD = "123456";
    //The maximum number of available connection instances is 8 by default. If - 1 is assigned, it means unlimited
    private static Integer MAX_TOTAL = 1024;
    //Controls the maximum number of jedis instances with idle status in a connection pool. The default value is 8
    private static Integer MAX_IDLE = 200;
    //The maximum waiting time for the available connection, in ms, with a default value of - 1, which means never timeout. If the waiting timeout occurs, a JedisConnectionException will be thrown
    private static Integer MAX_WAIT_MILLIS = 10000;
    private static Integer TIMEOUT = 10000;
    //When using a jedis instance, do you want to validate it in advance? If the result is true, the jedis instance is available
    private static Boolean TEST_ON_BORROW = true;
    //jedis connection pool
    private static JedisPool jedisPool = null;
     * Initializes the static block of the jedis connection pool, which is executed the first time the RedisPool class is loaded, and will not be executed later
    static {
        try {
            JedisPoolConfig conf = new JedisPoolConfig();
             * In the higher version of jedis jar, JedisPoolConfig does not have the setMaxActive and setMaxWait properties, because this method is officially enabled in the higher version of * and replaced with the following two properties
             * maxActive ==> maxTotal
             * maxWait ==> maxWaitMillis
            //Set the maximum number of connection instances
            //Set the maximum number of free jedis instances
            //Set the maximum time to wait for an available connection
            //Set whether to test borrowing in advance
            //New jedis connection pool
            jedisPool = new JedisPool(conf, ADDR, PORT, TIMEOUT, PWD);
        catch (Exception e) {
     * Get the jedis instance to operate the data, and return the connection to the connection pool after each use jedis.close()
     * @return
    public synchronized static Jedis getRedis() {
        try {
            if(jedisPool != null) {
                //Get jedis instance
                Jedis jedis = jedisPool.getResource();
                return jedis;
                System.out.println("Can't find Jedis Connection pool!");
                return null;
        } catch (Exception e) {
            return null;
     * It is used to recycle Jedis object resources. Users need to use this method to release resources. Otherwise, resources will be consumed all the time. In the new version, 'returnResource(jedis) will be discarded and is not recommended to be used. It is called directly` jedis.close(); ` return connection to connection pool.
     * @param Jedis jedis
    public synchronized static void returnJedis(Jedis jedis) {
        try {
            if(jedis != null) {
                //Reclaim jedis object resources
                System.out.println("Jedis Recycled successfully!");
        } catch (Exception e) {


This tutorial is hand-made, dedicated to the most practical tutorial, no reward, just hope to forward more support.
Welcome to my official account, I hope I can get to know you, and I can also urge you to search more. WeChat search: JavaPub

You can talk about any problem!

Topics: Java Jedis Redis JDBC