Java build Redis connection pool tool class

Redis is an open-source log and key value database written in ANSI C language, complying with BSD protocol, supporting network, memory based and persistent, and provides API s in multiple languages. During the encoding process, there may be a large amount of computation or fetching, which exceeds the timeout set by the HTTP protocol. This needs to be handled by redis.

First, we need to build a Redis server, which will not be introduced here. When the server is set up and the interface is written, how can we use it? Next, we will create a Redis connection pool to make it easier to use Redis. To create a Redis tool class, first import the jar, such as jedis-2.5.1.jar. The code is as follows:

public class RedisUtil{
  
   //Redis server IP
   private static String URL = "10.11.12.13";
   //Redis port number
   private static int PORT = 6379;
   //Maximum number of available connection instances, default is 8
   private static int MAX_ACTIVE = 1024;
   //Control the maximum number of jedis instances with idle status in a pool. The default value is also 8.
   private static int MAX_IDLE = 200;
   //The maximum time to wait for an available connection, in milliseconds. The default value is - 1, which means never timeout
   //If the waiting time is exceeded, a JedisConnectionException is thrown directly
   private static int MAX_WAIT = 20000;
   private static int TIMEOUT = 10000;
   //When a jedis instance is in borrow, do you want to validate it in advance? If it is true, the resulting jedis instances are available
   private static boolean TEST_ON_BORROW = true;
   
   private static JedisPool jedisPool = null;

   /**
    * Initialize Redis connection pool
    */
   static {
      try{
         JedisPoolConfig config = new JedisPoolConfig();
         config.setMaxIdle(MAX_ACTIVE);
         config.setMaxIdle(MAX_IDLE);
         config.setMaxWaitMillis(MAX_WAIT);
         config.setTestOnBorrow(TEST_ON_BORROW);
         jedisPool = new JedisPool(config, URL, PORT, TIMEOUT);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   /**
    * Get Jedis instance
    * @return
    */
   public synchronized static Jedis getJedis() {
        Jedis redis = null;
        try{
           if(jedisPool != null){
              redis = jedisPool.getResource();
              return redis;
           }else{
              return null;
           }
        } catch (Exception e){
             e.printStackTrace();
             return null;
        }
   }

   /**
    * Release jedis resources
    * @param jedis
    */
   public static void returnResource(final Jedis jedis){
       if(jedis != null){
           jedisPool.returnResource(jedis);
       }
   }

   /*
	* Test
	*/
   @Test
   public void main(){
       //Test the connection of Redis server
       Jedis jedis = new Jedis(URL,6379);
       System.out.println(jedis.ping());
       //Test Redis access (you need to determine the redis key storage type first, here is hashMap)
       Jedis jedis = getJedis();
       Map<String, String> ss = jedis.hgetAll("TEST");
   }
}

The above is the use of Redis in Java classes. Specific interfaces can be defined according to your own needs.

Tags: Programming Jedis Redis Database network

Posted on Mon, 25 Nov 2019 14:00:58 -0500 by CKPD