1. Jedis應用與基本連接
jedis 是 redis推薦的java客戶端。通過Jedis可以很方便地使用java代碼的方式,從而對redis進行操作。jedis使用起來比較簡單,它的操作方法與redis命令相類似。jedis在github上的下載地址為https://github.com/xetorthio/jedis 。如需要maven管理,可以添加如下依賴:
?
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.6.0</version> </dependency>
使用jedis進行連接:
public void connectionTest() {
jedis = new Jedis("127.0.0.1", 6379); // ip+port
jedis.auth("helloworld"); //password
}
關系型數據庫是基于關系表的數據庫,最終會將數據持久化到磁盤上,而nosql數據庫是基于特殊的結構,并將數據存儲到內存的數據庫。從性能上而言,nosql數據庫要優于關系型數據庫,從安全性上而言關系型數據庫要優于nosql數據庫,所以在實際開發中一個項目中nosql和關系型數據庫會一起使用,達到性能和安全性的雙保證。
redis就是典型的非關系型,可基于內存亦可持久化的日志型,Key-Value數據庫。為什么會出現NoSql數據庫?主要是對數據庫高并發讀寫的需求,每秒鐘上萬次的讀寫請求,硬盤IO無法承受。在部分場合(發紅包 獲取用戶的點擊信息 實時熱力圖)可以對關系數據庫起到很好的補充作用。
?
2. Jedis基本接口
public void keyTest() throws UnsupportedEncodingException {
// 清空數據
System.out.println(jedis.flushDB());
System.out.println(jedis.echo("hello"));
// 判斷key否存在
System.out.println(jedis.exists("foo"));
jedis.set("key", "values");
System.out.println(jedis.exists("key"));
// 如果數據庫沒有任何key,返回nil,否則返回數據庫中一個隨機的key。
String randomKey = jedis.randomKey();
System.out.println("randomKey: " + randomKey);
// 設置60秒后該key過期
jedis.expire("key", 60);
// key有效毫秒數
System.out.println(jedis.pttl("key"));
// 移除key的過期時間
jedis.persist("key");
// 獲取key的類型, "string", "list", "set". "none" none表示key不存在System.out.println("type: " + jedis.type("key"));
// 導出key的值byte[]
bytes = jedis.dump("key");System.out.println(new String(bytes));
// 將key重命名
jedis.renamenx("key", "keytest");
System.out.println("key是否存在: " + jedis.exists("key"));
// 刪除key
jedis.del("key");
System.out.println(jedis.exists("key"));
}
public void stringTest() {jedis.set("hello", "hello");System.out.println(jedis.get("hello"));// 使用append 向字符串后面添加jedis.append("hello", " world");System.out.println(jedis.get("hello"));// set覆蓋字符串jedis.set("hello", "123");System.out.println(jedis.get("hello"));// 設置過期時間jedis.setex("hello2", 2, "world2");System.out.println(jedis.get("hello2"));try {Thread.sleep(3000);} catch (InterruptedException e) {}System.out.println(jedis.get("hello2"));// 一次添加多個key-value對jedis.mset("a", "1", "b", "2");// 獲取a和b的valueList<String> valus = jedis.mget("a", "b");System.out.println(valus);// 批量刪除jedis.del("a", "b");System.out.println(jedis.exists("a"));System.out.println(jedis.exists("b"));
}
public void listTest() {// 隊列添加元素jedis.rpush(key, "aaaa");jedis.rpush(key, "bbbb");jedis.rpush(key, "cccc");// 隊列長度System.out.println("lenth: " + jedis.llen(key));// 打印隊列,從索引0開始,到倒數第1個(全部元素)System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 索引為1的元素,當index超出范圍時會返回一個errorSystem.out.println("index of 1: " + jedis.lindex(key, 1));// 從隊列的右邊入隊一個元素jedis.rpush(key, "-2", "-1");// 先-2,后-1入隊列System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 從隊列的左邊入隊一個或多個元素jedis.lpush(key, "second element", "first element");// 先second// 從隊列的右邊出隊一個元素System.out.println(jedis.rpop(key));// 從隊列的左邊出隊一個元素System.out.println(jedis.lpop(key));System.out.println("all elements: " + jedis.lrange(key, 0, -1));// count > 0: 從頭往尾移除值為 value 的元素,count為移除的個數。// count < 0: 從尾往頭移除值為 value 的元素,count為移除的個數。// count = 0: 移除所有值為 value 的元素。jedis.lrem(key, 1, "cccc");System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 刪除區間以外的元素System.out.println(jedis.ltrim(key, 0, 2));System.out.println("all elements: " + jedis.lrange(key, 0, -1));
}
?
總結
以上是生活随笔為你收集整理的[java进阶]2.Jedis基础与List的接口的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。