Jedis的基本使用

等著馬雲叫我去上班發表於2020-12-08

1.jedis官方文件

官方文件地址:http://xetorthio.github.io/jedis/

2.jedis常用API

api解釋
new Jedis(host, port)建立jedis物件,引數host是redis伺服器地址,引數port是redis服務埠
set(key,value)設定字串型別的資料
get(key)獲得字串型別的資料
hset(key,field,value)設定雜湊型別的資料
hget(key,field)獲得雜湊型別的資料
lpush(key,values)設定列表型別的資料
lpop(key)列表左面彈棧
rpop(key)列表右面彈棧
del(key)刪除指定的key

3.maven依賴座標

       <!-- jedis操作redis -->
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-pool2</artifactId>
           <version>2.3</version>
       </dependency>
       
       <dependency>
           <groupId>redis.clients</groupId>
           <artifactId>jedis</artifactId>
           <version>2.7.0</version>
       </dependency>

4.jedis常用操作

package com.alilaoye.web.day09.Jedis;

import redis.clients.jedis.Jedis;

/**
 * @program: basic_study
 * @description: Jedis基本操作
 * @author: 阿里姥爺
 * @create: 2020-12-07 20:57
 */
public class Demo02JedisBasicOperation {
    public static void main(String[] args) {
        //建立jedis物件
        Jedis jedis = new Jedis("localhost", 6379);

        //設定資料
        jedis.set("name", "張三");

        //取資料
        String name = jedis.get("name");
        System.out.println(name);

        //關閉連線
        jedis.close();
    }
}

5.jedisPool的基本使用

package com.alilaoye.web.day09.Jedis;

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

/**
 * @program: basic_study
 * @description: jedis連線池的基本操作
 * @author: 阿里姥爺
 * @create: 2020-12-07 21:02
 */
public class Demo03JedisPoolBasicOperation {
    public static void main(String[] args) {
        //建立jedis連線池配置物件
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(30); //設定最大連線數
        config.setMaxIdle(10);   //設定最大空閒連線數

        //建立jedis連線池物件
        JedisPool jedisPool = new JedisPool(config, "localhost", 6379);

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            String name1 = jedis.get("name");
            System.out.println(name1);

            jedis.set("name", "李四");

            String name2 = jedis.get("name");
            System.out.println(name2);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //釋放jedis和jedisPool資源
            if (jedis != null) {
                jedis.close();
                jedisPool.close();
            }
        }
    }
}

6.jedis工具類的抽取

jedis.properties配置檔案

#最大連線數
jedis.maxTotal=30
#最大空閒連線數
jedis.maxIdle=10
#連線地址
jedis.host=localhost
#連線埠
jedis.port=6379

jedis工具類

package com.alilaoye.web.day09.Jedis;

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

import java.util.ResourceBundle;

/**
 * @program: basic_study
 * @description: jedis工具類 【重要:ResourceBundle讀取配置檔案的使用】
 * @author: 阿里姥爺
 * @create: 2020-12-07 21:34
 */
public class Demo04JedisUtil {
    private static JedisPoolConfig jedisPoolConfig = null;
    private static JedisPool jedisPool = null;

    private static Integer maxTotal = null;
    private static Integer maxIdle = null;
    private static String host = null;
    private static Integer port = null;

    static {
        /*
        * ResourceBundle的使用
        *   1.JavaSE專案配置檔案(如jedis.properties)需放在src目錄下,JavaWeb專案需放在resource目錄下
        *   2.ResourceBundle.getBundle("jedis")時不用加字尾名
        * */
        //讀取配置檔案
        ResourceBundle resourceBundle = ResourceBundle.getBundle("jedis");
        maxTotal = Integer.parseInt(resourceBundle.getString("jedis.maxTotal"));
        maxIdle = Integer.parseInt(resourceBundle.getString("jedis.maxIdle"));
        host = resourceBundle.getString("jedis.host");
        port = Integer.parseInt(resourceBundle.getString("jedis.port"));

        jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);

        jedisPool = new JedisPool(jedisPoolConfig, host, port);
    }

    //靜態方法
    public static Jedis getJedis()
    {
        return jedisPool.getResource();
    }
}

測試jedis工具類

package com.alilaoye.web.day09.Jedis;

import redis.clients.jedis.Jedis;

/**
 * @program: basic_study
 * @description: 測試jedis工具類
 * @author: 阿里姥爺
 * @create: 2020-12-07 22:58
 */
public class Demo04JedisUtilTest {
    public static void main(String[] args) {
        Jedis jedis = Demo04JedisUtil.getJedis();

        jedis.set("jedisUtil","ok");

        System.out.println(jedis.get("jedisUtil"));
    }
}

7.ResourceBundle的使用

  1. JavaSE專案配置檔案(如jedis.properties)需放在src目錄下,JavaWeb專案需放在resource目錄下
  2. ResourceBundle.getBundle(“jedis”)時不用加字尾名.properties