redis客戶端--jedis


一、jedis

jedis 是 redis推薦的java客戶端。通過Jedis我們可以很方便地使用java代碼的方式,對redis進行操作。jedis使用起來比較簡單,它的操作方法與redis命令相類似。對於初次使用redis的人來說,上手更快,更能適應。jedis在github上的下載地址為https://github.com/xetorthio/jedis 。本例子使用maven,需要添加如下依賴:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.0</version> 
</dependency>

二、相關實例
  以下只是對數據的一些基本操作。
  獲取Jedis操作對象

1 Jedis jedis;
2 
3 @Before
4 public void connectionTest() {
5 jedis = new Jedis("127.0.0.1", 6379);//redis的地址以及連接端口
6 //jedis.auth("helloworld");  //開啟密碼驗證(配置文件中為 requirepass helloworld)的時候需要執行該方法
7
}

  Jedis對key的操作

 1 @Test
 2 public void keyTest() throws UnsupportedEncodingException {
 3 System.out.println(jedis.flushDB());// 清空數據
 4 System.out.println(jedis.echo("hello"));
 5 
 6 // 判斷key否存在
 7 System.out.println(jedis.exists("foo"));
 8 
 9 jedis.set("key", "values");
10 jedis.set("key2", "values");
11 System.out.println(jedis.exists("key"));// 判斷是否存在
12 
13 // 如果數據庫沒有任何key,返回nil,否則返回數據庫中一個隨機的key。
14 String randomKey = jedis.randomKey();
15 System.out.println("randomKey: " + randomKey);
16 
17 // 設置60秒后該key過期
18 jedis.expire("key", 60);
19 
20 // key有效毫秒數
21 System.out.println(jedis.pttl("key"));
22 
23 // 移除key的過期時間
24 jedis.persist("key");
25 
26 // 獲取key的類型, "string", "list", "set". "none" none表示key不存在
27 System.out.println("type: " + jedis.type("key"));
28 
29 // 導出key的值
30 byte[] bytes = jedis.dump("key");
31 System.out.println(new String(bytes));
32 
33 // 將key重命名
34 jedis.renamenx("key", "keytest");
35 System.out.println("key是否存在: " + jedis.exists("key"));// 判斷是否存在
36 System.out.println("keytest是否存在: " + jedis.exists("keytest"));// 判斷是否存在
37 
38 // 查詢匹配的key
39 // KEYS * 匹配數據庫中所有 key 。
40 // KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
41 // KEYS h*llo 匹配 hllo 和 heeeeello 等。
42 // KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
43 // 特殊符號用 \ 隔開。
44 Set<String> set = jedis.keys("k*");
45 System.out.println(set);
46 
47 // 刪除key
48 jedis.del("key");
49 System.out.println(jedis.exists("key"));
50 }

  Jedis對字符串(String)的相關操作

 1 @Test
 2 public void stringTest() {
 3 jedis.set("hello", "hello");
 4 System.out.println(jedis.get("hello"));
 5 
 6 // 使用append 向字符串后面添加
 7 jedis.append("hello", " world");
 8 System.out.println(jedis.get("hello"));
 9 
10 // set覆蓋字符串
11 jedis.set("hello", "123");
12 System.out.println(jedis.get("hello"));
13 
14 // 設置過期時間
15 jedis.setex("hello2", 2, "world2");
16 System.out.println(jedis.get("hello2"));
17 try {
18 Thread.sleep(3000);
19 } catch (InterruptedException e) {
20 }
21 System.out.println(jedis.get("hello2"));
22 
23 // 一次添加多個key-value對
24 jedis.mset("a", "1", "b", "2");
25 // 獲取a和b的value
26 List<String> valus = jedis.mget("a", "b");
27 System.out.println(valus);
28 
29 // 批量刪除
30 jedis.del("a", "b");
31 System.out.println(jedis.exists("a"));
32 System.out.println(jedis.exists("b"));
33 }

  Jedis對鏈表(Lists)的操作

 1 @Test
 2 public void listTest() {
 3 String key = "mylist";
 4 jedis.del(key);
 5 
 6 // 隊列添加元素
 7 jedis.rpush(key, "aaaa");
 8 jedis.rpush(key, "aaaa");
 9 jedis.rpush(key, "bbbb");
10 jedis.rpush(key, "cccc");
11 jedis.rpush(key, "cccc");
12 
13 // 隊列長度
14 System.out.println("lenth: " + jedis.llen(key));
15 
16 // 打印隊列,從索引0開始,到倒數第1個(全部元素)
17 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
18 
19 // 索引為1的元素
20 System.out.println("index of 1: " + jedis.lindex(key, 1));
21 
22 // 設置隊列里面一個元素的值,當index超出范圍時會返回一個error。
23 jedis.lset(key, 1, "aa22");
24 System.out.println("index of 1: " + jedis.lindex(key, 1));
25 
26 // 從隊列的右邊入隊一個元素
27 jedis.rpush(key, "-2", "-1");// 先-2,后-1入隊列
28 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
29 
30 // 從隊列的左邊入隊一個或多個元素
31 jedis.lpush(key, "second element", "first element");// 先second
32 // element,后first
33 // elementF入隊列
34 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
35 
36 // 從隊列的右邊出隊一個元素
37 System.out.println(jedis.rpop(key));
38 // 從隊列的左邊出隊一個元素
39 System.out.println(jedis.lpop(key));
40 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
41 
42 // count > 0: 從頭往尾移除值為 value 的元素,count為移除的個數。
43 // count < 0: 從尾往頭移除值為 value 的元素,count為移除的個數。
44 // count = 0: 移除所有值為 value 的元素。
45 jedis.lrem(key, 1, "cccc");
46 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
47 
48 // 即最右邊的那個元素也會被包含在內。 如果start比list的尾部下標大的時候,會返回一個空列表。
49 // 如果stop比list的實際尾部大的時候,Redis會當它是最后一個元素的下標。
50 System.out.println(jedis.lrange(key, 0, 2));
51 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
52 
53 // 刪除區間以外的元素
54 System.out.println(jedis.ltrim(key, 0, 2));
55 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
56 }

  Jedis對集合(Sets)的操作

@Test
public void testSet() {
// 清空數據
System.out.println(jedis.flushDB());
String key = "myset";
String key2 = "myset2";

// 集合添加元素
jedis.sadd(key, "aaa", "bbb", "ccc");
jedis.sadd(key2, "bbb", "ccc", "ddd");

// 獲取集合里面的元素數量
System.out.println(jedis.scard(key));

// 獲得兩個集合的交集,並存儲在一個關鍵的結果集
jedis.sinterstore("destination", key, key2);
System.out.println(jedis.smembers("destination"));

// 獲得兩個集合的並集,並存儲在一個關鍵的結果集
jedis.sunionstore("destination", key, key2);
System.out.println(jedis.smembers("destination"));

// key集合中,key2集合沒有的元素,並存儲在一個關鍵的結果集
jedis.sdiffstore("destination", key, key2);
System.out.println(jedis.smembers("destination"));

// 確定某個元素是一個集合的成員
System.out.println(jedis.sismember(key, "aaa"));

// 從key集合里面隨機獲取一個元素
System.out.println(jedis.srandmember(key));

// aaa從key移動到key2集合
jedis.smove(key, key2, "aaa");
System.out.println(jedis.smembers(key));
System.out.println(jedis.smembers(key2));

// 刪除並獲取一個集合里面的元素
System.out.println(jedis.spop(key));

// 從集合里刪除一個或多個元素
jedis.srem(key2, "ccc", "ddd");
System.out.println(jedis.smembers(key2));
}

  Jedis對有序集合(Sorted Sets)的操作

 1 @Test
 2 public void testSortSet() {
 3 // 清空數據
 4 System.out.println(jedis.flushDB());
 5 String key = "mysortset";
 6 
 7 Map<String, Double> scoreMembers = new HashMap<String, Double>();
 8 scoreMembers.put("aaa", 1001.0);
 9 scoreMembers.put("bbb", 1002.0);
10 scoreMembers.put("ccc", 1003.0);
11 
12 // 添加數據
13 jedis.zadd(key, 1004.0, "ddd");
14 jedis.zadd(key, scoreMembers);
15 
16 // 獲取一個排序的集合中的成員數量
17 System.out.println(jedis.zcard(key));
18 
19 // 返回的成員在指定范圍內的有序集合,以0表示有序集第一個成員,以1表示有序集第二個成員,以此類推。
20 // 負數下標,以-1表示最后一個成員,-2表示倒數第二個成員
21 Set<String> coll = jedis.zrange(key, 0, -1);
22 System.out.println(coll);
23 
24 // 返回的成員在指定范圍內的逆序集合
25 coll = jedis.zrevrange(key, 0, -1);
26 System.out.println(coll);
27 
28 // 元素下標
29 System.out.println(jedis.zscore(key, "bbb"));
30 
31 // 刪除元素
32 System.out.println(jedis.zrem(key, "aaa"));
33 System.out.println(jedis.zrange(key, 0, -1));
34 
35 // 給定值范圍內的成員數
36 System.out.println(jedis.zcount(key, 1002.0, 1003.0));
37 }

Jedis對哈希(Hashs)的操作

 1 @Test
 2 public void testHash() {
 3 // 清空數據
 4 System.out.println(jedis.flushDB());
 5 String key = "myhash";
 6 Map<String, String> hash = new HashMap<String, String>();
 7 hash.put("aaa", "11");
 8 hash.put("bbb", "22");
 9 hash.put("ccc", "33");
10 
11 // 添加數據
12 jedis.hmset(key, hash);
13 jedis.hset(key, "ddd", "44");
14 
15 // 獲取hash的所有元素(key值)
16 System.out.println(jedis.hkeys(key));
17 
18 // 獲取hash中所有的key對應的value值
19 System.out.println(jedis.hvals(key));
20 
21 // 獲取hash里所有元素的數量
22 System.out.println(jedis.hlen(key));
23 
24 // 獲取hash中全部的域和值,以Map<String, String> 的形式返回
25 Map<String, String> elements = jedis.hgetAll(key);
26 System.out.println(elements);
27 
28 // 判斷給定key值是否存在於哈希集中
29 System.out.println(jedis.hexists(key, "bbb"));
30 
31 // 獲取hash里面指定字段對應的值
32 System.out.println(jedis.hmget(key, "aaa", "bbb"));
33 
34 // 獲取指定的值
35 System.out.println(jedis.hget(key, "aaa"));
36 
37 // 刪除指定的值
38 System.out.println(jedis.hdel(key, "aaa"));
39 System.out.println(jedis.hgetAll(key));
40 
41 // 為key中的域 field 的值加上增量 increment
42 System.out.println(jedis.hincrBy(key, "bbb", 100));
43 System.out.println(jedis.hgetAll(key));
44 }

  Jedis操作事務

 1 @Test
 2 public void testTransaction() {
 3 Transaction t = jedis.multi();
 4 t.set("hello", "world");
 5 Response<String> response = t.get("hello");
 6 
 7 t.zadd("foo", 1, "barowitch");
 8 t.zadd("foo", 0, "barinsky");
 9 t.zadd("foo", 0, "barikoviev");
10 Response<Set<String>> sose = t.zrange("foo", 0, -1); //  返回全部相應並以有序集合的方式返回
11 System.out.println(response);
12 System.out.println(sose);
13 t.exec(); // 此行注意,不能缺少
14 
15 String foolbar = response.get(); // Response.get() 可以從響應中獲取數據
16 
17 int soseSize = sose.get().size(); // sose.get() 會立即調用set方法
18 System.out.println(foolbar);
19 System.out.println(sose.get());
20 }

  Jedis操作管道

 

 1     @Test
 2     public void testTransactionPipeling() {
 3         Pipeline p = jedis.pipelined();//開一個管道
 4 
 5         p.set("fool", "bar");
 6         p.zadd("foo", 1, "barowitch");
 7         p.zadd("foo", 0, "barinsky");
 8         p.zadd("foo", 0, "barikoviev");
 9         Response<String> pipeString = p.get("fool");
10         Response<Set<String>> sose = p.zrange("foo", 0, -1);
11         System.out.println(pipeString);
12         System.out.println(sose);
13 
14         p.sync();//提交
15 
16         System.out.println("==========");
17         System.out.println(p.get("fool"));
18         System.out.println(p.zrange("foo", 0, -1));
19 
20         int soseSize = sose.get().size();
21         Set<String> setBack = sose.get();
22 
23         System.out.println(soseSize);
24         System.out.println(setBack);
25     }

 


免責聲明!

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



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