Jedis operation Redis, Jedis connection pool tool class

Introduction of Jedis

​ Redis can not only use commands to operate, but now basically mainstream languages ​​have API support, such as Java, C#, C++, PHP, Node.js, Go, etc. Some Java clients are listed on the official website, including Jedis, Redisson, Jredis, JDBC-Redis, etc. Among them, Jedis and Redisson are officially recommended.

To use Jedis, you need to import the jar package

Insert picture description here
Link: https://pan.baidu.com/s/1zuVLD-8VO6v08dCMF8TfyA
Extraction code: xe81 After
copying this content, open the Baidu Netdisk mobile phone App, the operation is more convenient

Common methods of Jedis class

Note: Each method is the command name in redis, and the parameters of the method are the parameters of the command. Each Jedis object is similar to the Connection object in JDBC.
Insert picture description here

For specific commands, please see another article of mine: Redis Overview and Common Commands
https://blog.csdn.net/RookiexiaoMu_a/article/details/89609874

Case : Use the above method of Jedis to access Redis, write the string and list type to the server, and take it out to the print console.

import redis.clients.jedis.Jedis;

import java.util.List;

/*
使用Jedis的方法来访问Redis,向服务器中写入字符串和list类型,并且取出打印控制台上
 */
public class Demo1 {
    
    

    public static void main(String[] args) {
    
    
        //1. 创建连接Jedis
        //参数:主机名或IP地址, 端口号
        Jedis jedis = new Jedis("localhost", 6379);
        //2. 通过jedis对象中方法向服务器写入字符串的类型
        jedis.set("person","张三");
        //3. 通过jedis对象中方法向服务器写入list的类型
        jedis.lpush("cities","广州","上海","东莞");
        //4. 通过方法取出字符串和list打印
        String person = jedis.get("person");
        List<String> cities = jedis.lrange("cities", 0, -1);
        System.out.println(person);
        System.out.println(cities);
        //5. 关闭jedis连接
        jedis.close();
    }
}

The basic concept of Jedis connection pool

Insert picture description here
The creation and destruction of jedis connection resources consumes program performance, so jedis provides us with jedis connection pool technology. When jedis connection pool is created, some connection objects are initialized and stored in the connection pool. When using jedis connection resources, you do not need to create it yourself. The jedis object instead obtains a resource from the connection pool to perform redis operations. After use, there is no need to destroy the jedis connection resource, but return the resource to the connection pool for other requests.

Jedis connection pool API

Insert picture description here
Case : Use the connection pool to optimize the jedis operation, get a created Jeids object from the connection pool, and use this Jedis object. Write a set collection to the Redis database, and remove the collection. Print to the console and view the information in the database.

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Set;

/*
1. 使用连接池优化jedis操作,从连接池中得到一个创建好的Jeids对象,并且使用这个Jedis对象。
2. 向Redis数据库写入一个set集合,并且取出集合。打印到控制台,并且查看数据库中信息。
 */
public class Demo2 {
    
    

    public static void main(String[] args) {
    
    
        //1. 创建配置对象,指定2个参数:最大连接数,最长等待时间
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10);
        config.setMaxWaitMillis(2000);  //毫秒
        //2. 创建连接池:指定3个参数:上面的配置对象,主机名,端口号
        JedisPool pool = new JedisPool(config, "localhost", 6379);
        //3. 从连接池中得到连接Jedis对象: getResource()
        Jedis jedis = pool.getResource();

        //4. 向redis中写入一个set集合: sadd
        jedis.sadd("students","白骨精","孙悟空","猪八戒");
        //5. 从redis中取出这个set集合打印出来: smembers
        Set<String> students = jedis.smembers("students");
        System.out.println(students);
        //6. 关闭jedis
        jedis.close();
    }

}

ResourceBundle class

Use ResourceBundle to read configuration files
Insert picture description here

//1. 得到ResourceBundle类
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
//2. 调用getString("键")得到值
String host = bundle.getString("host");

Case: Writing jedis connection pool tool class

Writing steps:

  1. Use ResourceBundle to read configuration files
  2. Create Jedis connection pool configuration object and specify parameters
  3. Create a connection pool object
  4. Write a method to obtain the Jedis object

JedisUtils.java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

/*
工具类:
1. 简化得到连接代码
2. 修改配置更加方便
 */
public class JedisUtils {
    
    

    //定义成全局的变量
    private static JedisPool pool;

    //在类加载的时候就创建连接池
    static  {
    
    
        //读取所有的配置参数
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        String host = bundle.getString("host");
        int maxTotal = Integer.parseInt(bundle.getString("maxTotal"));
        int maxWaitMillis = Integer.parseInt(bundle.getString("maxWaitMillis"));
        int port = Integer.parseInt(bundle.getString("port"));

        //1. 创建配置对象,指定2个参数:最大连接数,最长等待时间
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxWaitMillis(maxWaitMillis);  //毫秒
        //2. 创建连接池:指定3个参数:上面的配置对象,主机名,端口号
        pool = new JedisPool(config, host, port);
    }

    /**
     得到连接对象
     @return
     */
    public static Jedis getJedis() {
    
    
        //3. 从连接池中得到连接Jedis对象: getResource()
        return pool.getResource();
    }
}

Guess you like

Origin blog.csdn.net/RookiexiaoMu_a/article/details/89614277