Jedis工具類使用及設置


Jedis是Redis數據庫的java工具類,類似於JDBC中的Connection,也是對數據庫進行CRUD的通道(這樣說是不是有點不嚴謹~~~)

附上幾個Redis的通用命令:

key* 查看所有的key值

type   key 獲得key的數據類型

del key [key.....]
        可以刪除一個或多個鍵,返回值是刪除的鍵的個數
exists key 判斷key是否存在

Jedis一般工具類的編寫,如圖:(此處以本機為數據庫)

代碼如下:

 1 package com.didinx.filter;
 2 
 3 import redis.clients.jedis.Jedis;
 4 
 5 /**
 6  * @author o_0sky
 7  * @date 2019/2/14 1:07
 8  */
 9 public class Jedis1 {
10     public static void main(String[] args) {
11         //創建redis核心對象
12         Jedis jedis = new Jedis("localhost", 6379);
13         //存值
14         jedis.set("key", "value");
15         //取值並打印
16         System.out.println(jedis.get("key"));
17         //釋放資源
18         jedis.close();
19     }
20 }

 jedis如同connection一樣,連接資源的創建和銷毀都會很消耗性能,所以jedis提供了池化技術,下圖是常用的池化技術的參數列表:

聯系上圖:

 

池化技術代碼如下:

 

 1 package com.didinx.filter;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 import redis.clients.jedis.JedisPoolConfig;
 6 
 7 /**
 8  * @author o_0sky
 9  * @date 2019/2/14 1:33
10  */
11 public class JedisPool1 {
12     public static void main(String[] args) {
13         //生成連接池配置對象,設置配置項
14         JedisPoolConfig config = new JedisPoolConfig();
15         //最大連接數
16         config.setMaxTotal(100);
17         //最大空閑數
18         config.setMaxIdle(20);
19         //獲得連接池
20         JedisPool jedisPool = new JedisPool(config, "localhost", 6379);
21         Jedis jedis = null;
22         try {
23             //從連接池獲取連接
24             jedis = jedisPool.getResource();
25             //給連接賦值
26             jedis.set("age", "30");
27             //從連接池中取值並打印
28             System.out.println(jedis.get("age"));
29         } catch (Exception e) {
30             e.printStackTrace();
31         } finally {
32             //此處jedis.close()並不是銷毀連接,而是將連接返回連接池
33             if (null != jedis) {
34                 jedis.close();
35             }
36         }
37         //電腦關閉時,關閉連接池
38         if (null != jedisPool) {
39             jedisPool.close();
40         }
41 
42     }
43 }

如同JDBC,Jedis也有工具類,配置文件為properties格式,代碼如下:

 1 package didinx;
 2 
 3 import redis.clients.jedis.Jedis;
 4 import redis.clients.jedis.JedisPool;
 5 import redis.clients.jedis.JedisPoolConfig;
 6 
 7 import java.util.ResourceBundle;
 8 
 9 /**
10  * @author o_0sky
11  * @date 2019/2/14 1:49
12  */
13 public class JedisUtil {
14     //設置參數
15     private static String host;
16     private static int port;
17     private static int maxtotal;
18     private static int maxwaitmillis;
19     private static JedisPool jedisPool;
20 
21     //加載配置文件並給參數賦值
22     static {
23         ResourceBundle rb = ResourceBundle.getBundle("jedis");
24         maxtotal = Integer.parseInt(rb.getString("maxtotal"));
25         maxwaitmillis = Integer.parseInt(rb.getString("maxwaitmillis"));
26         port = Integer.parseInt(rb.getString("port"));
27         host = rb.getString("host");
28     }
29 
30     /**
31      * 初始化連接池
32      */
33     static {
34         JedisPoolConfig config = new JedisPoolConfig();
35         config.setMaxTotal(maxtotal);
36         config.setMaxWaitMillis(maxwaitmillis);
37         jedisPool = new JedisPool(config, host, port);
38     }
39 
40     /**
41      * 獲取連接方法
42      *
43      * @return
44      */
45     public static Jedis getJedis() {
46         return jedisPool.getResource();
47     }
48 
49     /**
50      * 釋放資源
51      *
52      * @param jedis
53      */
54     public static void closeJedis(Jedis jedis) {
55         if (null != jedis) {
56             jedis.close();
57         }
58     }
59 }

 


免責聲明!

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



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