1. 程式人生 > 其它 >Java操作Redis—Jedis

Java操作Redis—Jedis

之所以使用Jedis,是因為它操作redis時使用的方法名與redis命令一致。

一、簡單使用

1.1、新建maven專案

新建一個maven專案,專案結構如下:

1.2、匯入依賴

<dependencies>
    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

</dependencies>

1.3、簡單測試

public class Demo {

    @Test
    public void set() {
        // 開啟連線
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        // 執行操作
        jedis.set("name", "李四");
        // 關閉連線
        jedis.close();
    }

    @Test
    public void get() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        String name = jedis.get("name");
        System.out.println(name);
        jedis.close();
    }
}

程式執行結果過:

使用RDM檢視:

2、儲存物件

想將一個物件儲存到redis中,需要先將其轉化為位元組陣列或者json字串。

2.1、匯入依賴

<!-- 物件序列化工具 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.3</version>
</dependency>

<!-- fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

2.2、建立一個實體類

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    private String name;
    private int age;
    private char sex;
    private Date birthday;
    
}

2.3、操作測試

2.3.1、byte陣列方式
@Test
public void setObjByByte() {
    Jedis jedis = new Jedis("127.0.0.1", 6379);

    String key = "zhangsan";
    Person person = new Person("張三", 13, '1', new Date());

    byte[] keyBytes = key.getBytes();
    byte[] personBytes = SerializationUtils.serialize(person);

    jedis.set(keyBytes, personBytes);
    jedis.close();
}

@Test
public void getObjByByte() {
    Jedis jedis = new Jedis("127.0.0.1", 6379);

    String key = "zhangsan";
    byte[] keyBytes = key.getBytes();
    byte[] bytes = jedis.get(keyBytes);

    Person person = (Person) SerializationUtils.deserialize(bytes);
    System.out.println(person);
    jedis.close();
}
2.3.2、json字串方式
@Test
public void getObjByByte() {
    Jedis jedis = new Jedis("127.0.0.1", 6379);

    String key = "zhangsan";
    byte[] keyBytes = key.getBytes();
    byte[] bytes = jedis.get(keyBytes);

    Person person = (Person) SerializationUtils.deserialize(bytes);
    System.out.println(person);
    jedis.close();
}

@Test
public void setObjByJson() {
    Jedis jedis = new Jedis("127.0.0.1", 6379);

    String key = "zhangsan";
    Person person = new Person("張三", 13, '1', new Date());

    jedis.set(key, JSON.toJSONString(person));

    jedis.close();
}

@Test
public void getObjByJson() {
    Jedis jedis = new Jedis("127.0.0.1", 6379);

    String key = "zhangsan";
    String person = jedis.get(key);

    System.out.println(person);
    jedis.close();
}

3、Jedis連線池

3.1、預設配置方式

Jedis預設連線池配置如下:

public class GenericObjectPoolConfig extends BaseObjectPoolConfig {
    public static final int DEFAULT_MAX_TOTAL = 8;
    public static final int DEFAULT_MAX_IDLE = 8;
    public static final int DEFAULT_MIN_IDLE = 0;
    private int maxTotal = 8;
    private int maxIdle = 8;
    private int minIdle = 0;
	
    ...
}    
@Test
public void testPool1() {
    // 預設配置連線池
    JedisPool pool = new JedisPool("127.0.0.1", 6379);

    // 獲取Jedis連線資源
    Jedis jedis = pool.getResource();

    // 執行操作
    String key = "zhangsan";
    String person = jedis.get(key);
    System.out.println(person);
    
    //關閉連線
    jedis.close();
}

3.2、自定義配置

@Test
public void testPool2() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(100); //連線池容量
    poolConfig.setMaxIdle(10); //最大空閒連線數
    poolConfig.setMinIdle(0); //最小空閒連線數
    poolConfig.setMaxWaitMillis(2000); //最大超時等待時間,當連線池空閒數為0時,程式等待的最大時間。 單位 ms
    
    JedisPool pool = new JedisPool(poolConfig, "127.0.0.1", 6379);

    Jedis jedis = pool.getResource();

    String key = "zhangsan";
    String person = jedis.get(key);

    System.out.println(person);
    
    jedis.close();
}

4、Redis管道

當客戶端有大量重複命令提交到伺服器時,為了提升執行效率,Redis提供了管道的操作方式。

具體使用方法及管道使用前後對比如下:

@Test
public void testPipe() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(100);
    poolConfig.setMaxIdle(10);
    poolConfig.setMinIdle(0);
    poolConfig.setMaxWaitMillis(2000);
    JedisPool pool = new JedisPool(poolConfig, "127.0.0.1", 6379);

    Jedis jedis = pool.getResource();

    // 獲取管道
    Pipeline pipeline = jedis.pipelined();

    // 程式開始執行時間戳
    long begin = System.currentTimeMillis();
    // 不使用管道,執行自增10萬次自增
    for (int i = 0; i < 100000; i++) {
        jedis.incr("sth");
    }
    long end = System.currentTimeMillis();
    System.out.println("without pipe: " + (end - begin));

    // 程式開始執行時間戳
    long begin1 = System.currentTimeMillis();
    // 使用管道,執行自增10萬此
    for (int i = 0; i < 100000; i++) {
        pipeline.incr("foo");
    }
    long end1 = System.currentTimeMillis();
    System.out.println(end1 - begin1);

    jedis.close();

}

程式耗時對比: