【Redis】使用Jedis操作Redis


Jedis介紹

  jedis就是集成了redis的一些命令操作,封裝了redis的java客戶端。

Jedis使用

  使用jedis需要引入jedis的jar包,下面提供了maven依賴

  jedis.jar是封裝的包,commons-pool2.jar是管理連接的包

 1 <!-- https://mvnrepository.com/artifact/redis.clients/jedis 客戶端-->
 2         <dependency>
 3             <groupId>redis.clients</groupId>
 4             <artifactId>jedis</artifactId>
 5             <version>2.9.0</version>
 6         </dependency>
 7 
 8         <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
 9         <dependency>
10             <groupId>org.apache.commons</groupId>
11             <artifactId>commons-pool2</artifactId>
12             <version>2.5.0</version>
13         </dependency>

  1、新建一個maven工程,引入上面的依賴,編寫測試類,如下

  

 1 package com.test.jedis;
 2 
 3 import org.junit.Test;
 4 
 5 import redis.clients.jedis.Jedis;
 6 import redis.clients.jedis.JedisPool;
 7 import redis.clients.jedis.JedisPoolConfig;
 8 
 9 public class TestJedisDemo1 {
10 
11     /**
12      * 單實例連接redis數據庫
13      * @Description TODO
14      * @author H__D
15      * @date 2018年7月5日
16      */
17     @Test
18     public void run1() {
19         
20         Jedis jedis = new Jedis("127.0.0.1", 6379);
21         jedis.set("sex", "男");
22         System.out.println(jedis.get("sex"));
23     }
24 
25     /**
26      * Jedis連接池
27      * @Description TODO
28      * @author H__D
29      * @date 2018年7月5日
30      */
31     @Test
32     public void run2() {
33         
34         // 1、設置連接池的配置對象
35         JedisPoolConfig config = new JedisPoolConfig();
36         // 設置池中最大的連接數量(可選)
37         config.setMaxTotal(50);
38         // 設置空閑時池中保有的最大連接數(可選)
39         config.setMaxIdle(10);
40         
41         // 2、設置連接池對象
42         JedisPool pool = new JedisPool(config, "127.0.0.1", 6379);
43         
44         // 3、從池中獲取連接對象
45         Jedis jedis = pool.getResource();
46         System.out.println(jedis.get("sex"));
47         
48         // 4、連接池歸還
49         jedis.close();
50     }
51 }

  

  2、編寫連接池工具類

  

 1 package com.test.jedis;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 import redis.clients.jedis.JedisPoolConfig;
 6 
 7 public class JedisUtils {
 8 
 9     // 1、定義一個連接池對象
10     private final static JedisPool POOL;
11 
12     static {
13         // 初始化
14         // 1、設置連接池的配置對象
15         JedisPoolConfig config = new JedisPoolConfig();
16         // 設置池中最大的連接數量(可選)
17         config.setMaxTotal(50);
18         // 設置空閑時池中保有的最大連接數(可選)
19         config.setMaxIdle(10);
20 
21         // 2、設置連接池對象
22         POOL = new JedisPool(config, "127.0.0.1", 6379);
23     }
24     
25     /**
26      * 從連接池中獲取連接
27      * @Description TODO
28      * @author H__D
29      * @date 2018年7月5日
30      * @return
31      */
32     public static Jedis getJedis() {
33         return POOL.getResource();
34     }
35 }

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM