Redis quick start (three): jedis connection pool & related tools

1. Overview of jedis

Jedis is the official Java connection development tool recommended by Redis. To use Redis middleware in Java development, you must be familiar with Jedis to write beautiful code.

2. Basic use of jedis

The basic use of Jedis is very simple, just specify the host (host name), port (port), password (password) when creating the Jedis object. Of course, there are many construction methods for Jedis objects, all of which are similar, except that the parameters corresponding to the socket connected to Redis are different:

import redis.clients.jedis.Jedis;

public class JedisTest {
    
    


    public static Jedis init(String host,Integer port){
    
    
        Jedis jedis = new Jedis(host, port);
        return jedis;
    }

    public static Jedis init(String host){
    
    
        Jedis jedis = new Jedis(host);
        return jedis;
    }

    public static Jedis init(String host,Integer port,String password){
    
    
        Jedis jedis = new Jedis(host, port);
        jedis.auth(password);
        return jedis;
    }
}

3. Connection pool usage

The jedis connection pool is based on apache-commons pool2. When constructing a connection pool object, you need to provide the configuration object of the pool object, and JedisPoolConfig (inherited from GenericObjectPoolConfig). We can configure the relevant parameters of the connection pool through this configuration object (such as the maximum number of connections, the maximum number of empty, etc.).

* 使用:
1.创建JedisPool连接池对象 
2.调用方法 getResource()方法获取Jedis连接
//0.创建一个配置对象 
JedisPoolConfig config = new JedisPoolConfig(); 
config.setMaxTotal(50); 
config.setMaxIdle(10); //最大空闲时间

 //1.创建Jedis连接池对象
		        JedisPool jedisPool = new 		
		        JedisPool(config,"localhost",6379);
		
		        //2.获取连接
		        Jedis jedis = jedisPool.getResource();
		        //3. 使用
		        jedis.set("hehe","heihei");
				//4. 关闭 归还到连接池中
				 jedis.close();
2. Tools
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisPoolUtils {
    
    

    private static JedisPool jedisPool;

    static {
    
    
        //读取配置文件
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        //创建Properties对象
        Properties pro = new Properties();
        //关联文件
        try {
    
    
            pro.load(is);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        //获取数据,设置到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));

        //初始化JedisPool
        jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
}

                    /**
 				     * 获取连接方法
 				     */
        

    public static Jedis getJedis() {
    
    
		return jedisPool.getResource();
	}

}

The previous chapter: Redis Quick Start (2): Five common data structures of redis
Next chapter: With the new. .

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/109355035
Recommended