redis client端示例代碼

redis提供了幾乎所有主流語言的client,Java中主要使用二種:Jedis與Redisson

一、Jedis的使用

複製代碼
1         <dependency>
2             <groupId>redis.clients</groupId>
3             <artifactId>jedis</artifactId>
4             <version>2.6.0</version>
5             <type>jar</type>
6             <scope>compile</scope>
7         </dependency>
複製代碼

pom.xml中添加上面的依賴項就行了,下面是示例代碼:

複製代碼
 1     @Test
 2     public void testJedis() throws InterruptedException {
 3 
 4         Jedis jedis = new Jedis("10.6.144.xxx", 7030);
 5 
 6         // String測試
 7         System.out.println(jedis.get("foo")); // 首次獲取,redis中還沒有,返回null
 8 
 9         jedis.set("foo", "bar", "NX", "EX", 1);// 添加緩存項foo,過期時間爲1s,只有該項原來不存在時,才添加
10 
11         System.out.println(jedis.get("foo"));// bar
12 
13         Thread.sleep(1000);// 暫停1s
14 
15         System.out.println(jedis.get("foo"));// 已過期,返回null
16 
17         jedis.close();
18     }
複製代碼

輸出:

null
bar
null

Jedis的特點:可以使用String這種簡單類型,可以設置緩存過期時間,但對於集合的使用不太方便。

另外有一個十分有用的命令: keys

比如./redis-cli keys \* 將返回所有的緩存keys,可用於遍歷所有緩存項,Jedis支持keys命令:

        Set<String> keys= jedis.keys("*");
        for (String k : keys) {
            System.out.println(k);
        }

 

二、Redisson的使用

1         <dependency>
2             <groupId>org.redisson</groupId>
3             <artifactId>redisson</artifactId>
4             <version>1.1.5</version>
5         </dependency>

同樣先添加依賴項,然後參考下面的示例代碼:

複製代碼
 1     @Test
 2     public void testRedisson() {
 3         Config config = new Config();
 4         config.useSingleServer().setAddress("10.6.144.***:7030");
 5 
 6         Redisson redisson = Redisson.create(config);
 7 
 8         // Set測試
 9         RSet<String> mySet = redisson.getSet("mySet");
10         if (mySet != null) {
11             mySet.clear();
12         }
13         mySet.add("1");
14         mySet.add("2");
15         mySet.add("3");
16 
17         RSet<String> mySetCache = redisson.getSet("mySet");
18 
19         for (String s : mySetCache) {
20             System.out.println(s);
21         }
22 
23         System.out.println("--------------------");
24 
25         // List測試
26         RList<SampleBean> myList = redisson.getList("myList");
27         if (myList != null) {
28             myList.clear();
29         }    
30         
31         myList.add(new SampleBean("A"));
32         myList.add(new SampleBean("B"));
33         myList.add(new SampleBean("C"));
34 
35         RList<SampleBean> myListCache = redisson.getList("myList");
36 
37         for (SampleBean bean : myListCache) {
38             System.out.println(bean);
39         }
40         
41         System.out.println("--------------------");
42         
43         //Queue測試
44         RQueue<String> myQueue = redisson.getQueue("myQueue");
45         if (myQueue != null) {
46             myQueue.clear();
47         }
48         myQueue.add("X");
49         myQueue.add("Y");
50         myQueue.add("Z");
51         
52         RQueue<String> myQueueCache = redisson.getQueue("myQueue");
53 
54         for (String s : myQueueCache) {
55             System.out.println(s);
56         }
57         
58         System.out.println("--------------------");
59         
60         System.out.println(myQueue.size());//3
61         System.out.println(myQueue.poll());//X
62         System.out.println(myQueue.size());//2
63         
64         System.out.println("--------------------");
65         
66         //注:雖然是從myQueue中poll的,但是由於myQueueCache與myQueue實際上是同一個緩存對象,所以下面的循環,也只剩2項
67         for (String s : myQueueCache) {
68             System.out.println(s);
69         }        
70         System.out.println("--------------------");
71         
72         //Deque測試
73         RDeque<String> myDeque = redisson.getDeque("myDeque");
74         if (myDeque != null) {
75             myDeque.clear();
76         }
77         myDeque.add("A");
78         myDeque.add("B");
79         myDeque.add("C");
80         
81         RDeque<String> myDequeCache = redisson.getDeque("myDeque");
82 
83         Iterator<String> descendingIterator = myDequeCache.descendingIterator();
84         
85         //倒序輸出
86         while (descendingIterator.hasNext()) {
87             System.out.println(descendingIterator.next());
88             
89         }
90         
91         redisson.shutdown();
92     }
93 
94 }
複製代碼

注:List中可直接放POJO對象,上面的示例中放了3個SampleBean實例,SampleBean定義如下:

複製代碼
 1 package com.cnblogs.yjmyzz;
 2 
 3 import java.io.Serializable;
 4 
 5 public class SampleBean implements Serializable {
 6 
 7     
 8     private static final long serialVersionUID = -303232410998377570L;
 9 
10     private String name;
11 
12     public SampleBean() {
13     }
14 
15     public SampleBean(String name) {
16         this.name = name;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String toString() {
28         return name;
29     }
30 
31 }
複製代碼

輸出:

3
1
2
--------------------
A
B
C
--------------------
X
Y
Z
--------------------
3
X
2
--------------------
Y
Z
--------------------
C
B
A

Redisson的優勢:對集合支持比較完善,但是不支持String這類簡單類型,不支持keys操作

實際使用中,上述二種client建議結合使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章