JavaWeb [Redis] Jedis reads and writes redis and connection pool

JavaWeb [Redis] Jedis reads and writes redis and connection pool

Jedis introduction

  • (1) What is Jedis?
    Tools for operating Redis: Jedis
    "" Redis: the name of the database
    " "Jedis: the name of the tool.
    Jedis is also hosted on github, address: https://github.com/xetorthio/jedis .
  • (2) How to build Jedis? To
    use Jedis to operate redis, you need to import the jar package as follows
    Insert picture description here

Jedis operation

  • (1) How to use Jedis?
    Operation steps:
    "1: Create a Jedis object
    " "2: Execute Redis instructions
       //1:创建Jedis对象
        Jedis jedis = new Jedis("localhost", 6379);
        //2:调用方法执行Redis指令(每一条指令,都有一个方法)
        jedis.set("name", "wzx");
        jedis.sadd("mylist","aaa","bbb","ccc");

        String addr = jedis.get("addr");
        System.out.println("addr:"+addr);
12345678

Insert picture description here

Jdbc connection pool principle

  • (1) When do you contact the connection pool?
    Jdbc connection pool
  • (2) Connection pool principle
    Frequent creation and destruction of links consumes performance.
    Create a collection and initialize multiple Connections . Call
    getConnection() when needed
    . Call close() and put it back when you are done using it.
  • (3) The significance of the connection pool
    Reuse connections and improve performance

Insert picture description here

Basic implementation of Jedis connection poolOne

  • (1) The principle of Jedis connection pool is
    basically similar
    Insert picture description here
  • (2) Code implementation step
    1: Create the configuration object of the connection pool
    2: Create the connection pool
    3: Get a connection from the connection pool
    4: Execute Redis execute Map<String,String> set get
    5: Release the connection
       //1:创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
         //1.1 设置最大连接数
        config.setMaxTotal(30);
        //1.2 设置空闲连接数
        config.setMaxIdle(10);

        //2:创建连接池
        JedisPool pool = new JedisPool(config, "localhost", 6379);

        //3:从连接池中获取一个连接
        Jedis jedis = pool.getResource();

        //4:执行Redis执行
        jedis.set("email", "[email protected]");

        //5:释放连接
        jedis.close();
123456789101112131415161718

Basic implementation of Jedis connection poolOne

  • (1) Hard-coded The
    configuration parameters are written in java code and compiled into a class. You must modify the java code in the future to change the configuration. In the
    project, use the configuration file to configure the parameters
  • (2) Change to tools plus configuration files

src\com.wzx.pack01_redis\Demo3JedisPool.java

public class Demo3JedisPool {
    
    
    @Test
    public void test01() {
    
    
        Jedis jedis = JedisUtils.getJedis();//调用静态方法
        jedis.set("xxx", "yyyyy");
        jedis.close();
    }
}
12345678

src\jedis.properties

  • The properites file configures the parameters, and then the program reads these configuration parameters for the object to use. This is the way that JAVAEE often uses
maxTotal=30
maxIdle=10
url=localhost
port=6379
1234

src \ com.wzx.pack01_redis \ JedisUtils.java

To read the .properties file, you can first use the class loader to load it into a stream, and then use the Properties class to read it.
You can also use ResourceBundleread.propties

public class JedisUtils {
    
    
    private static JedisPool pool = null;
    //1:创建一个连接池
    static{
    
    
        //1.1 解析properties文件
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        //获取参数
        String maxTotal = bundle.getString("maxTotal");
        String maxIdle = bundle.getString("maxIdle");
        String url = bundle.getString("url");
        String port = bundle.getString("port");

        //1.2创建连接池
        //1:创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //1.1 设置最大连接数
        config.setMaxTotal(Integer.parseInt(maxTotal));
        //1.2 设置空闲连接数
        config.setMaxIdle(Integer.parseInt(maxIdle));

        //2:创建连接池
         pool = new JedisPool(config, url, Integer.parseInt(port));
    }

    //2:对外提供一个获取连接的方法
    public static Jedis getJedis(){
    
    
        return  pool.getResource();
    }

    //3:提供释放资源的方法
    public static void close(Jedis jedis){
    
    
        if(jedis != null) {
    
    
            jedis.close();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108737291
Recommended