演算法題-組合【JS實現】
阿新 • • 發佈:2021-01-16
Jedis
前言
提示:本文章是日常學習內容的總結,並非全部原創;僅供大家參考借鑑,並無其他商業用途。Bilibili搜尋關注:狂神說
真正在公司中的實踐:NoSQL + RDBMS 一起使用才是最強的,阿里巴巴的架構演進!
技術沒有高低之分,就看你如何去使用!(提升內功,思維的提高!)
雲端計算的長征之路:阿里雲的這群瘋子
什麼是Jedis
是Jedis 是 Redis 官方推薦的 java連線開發工具!
是使用Java 操作Redis 中介軟體!
如果你要使用java去操作redis
那麼一定要對Jedis 十分的熟悉!
為什麼要使用Jedis
知其然並知其所以然,授人以漁! 學習不能急躁,慢慢來會很快!
先了解Jedis,再用spring-data-redis去整合
所以我們要先使用 Java 來操作 Redis,
1、匯入對應的依賴
<!--匯入jedis的包-->
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
< artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
2、編碼測試
1. 連線資料庫
2. 操作命令
3. 斷開連線!
package com.kuang;
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
// new Jedis 物件即可
Jedis jedis = new Jedis("127.0.0.1",6379);
// jedis 所有的命令就是redis-cli客戶端的所有指令!
System.out.println(jedis.ping());
}
}
4. 輸出
3、常用的API
所有的api命令,都對應前面文章的五大資料型別的指令,一個都沒有變化!
3.1 String
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("===========增加資料===========");
System.out.println(jedis.set("key1","value1"));
System.out.println(jedis.set("key2","value2"));
System.out.println(jedis.set("key3", "value3"));
System.out.println("刪除鍵key2:"+jedis.del("key2"));
System.out.println("獲取鍵key2:"+jedis.get("key2"));
System.out.println("修改key1:"+jedis.set("key1", "value1Changed"));
System.out.println("獲取key1的值:"+jedis.get("key1"));
System.out.println("在key3後面加入值:"+jedis.append("key3", "End"));
System.out.println("key3的值:"+jedis.get("key3"));
System.out.println("增加多個鍵值對:"+jedis.mset("key01","value01","key02","value02","key03","value03"));
System.out.println("獲取多個鍵值對:"+jedis.mget("key01","key02","key03"));
System.out.println("獲取多個鍵值對:"+jedis.mget("key01","key02","key03","key04"));
System.out.println("刪除多個鍵值對:"+jedis.del("key01","key02"));
System.out.println("獲取多個鍵值對:"+jedis.mget("key01","key02","key03"));
jedis.flushDB();
System.out.println("===========新增鍵值對防止覆蓋原先值==============");
System.out.println(jedis.setnx("key1", "value1"));
System.out.println(jedis.setnx("key2", "value2"));
System.out.println(jedis.setnx("key2", "value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("===========新增鍵值對並設定有效時間=============");
System.out.println(jedis.setex("key3", 2, "value3"));
System.out.println(jedis.get("key3"));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.get("key3"));
System.out.println("===========獲取原值,更新為新值==========");
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("獲得key2的值的字串:"+jedis.getrange("key2", 2, 4));
}
3.2 List
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("===========新增一個list===========");
jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", "TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));//-1代表倒數第一個元素,-2代表倒數第二個元素,end為-1表示查詢全部
System.out.println("collections區間0-3的元素:"+jedis.lrange("collections",0,3));
System.out.println("===============================");
// 刪除列表指定的值 ,第二個引數為刪除的個數(有重複時),後add進去的值先被刪,類似於出棧
System.out.println("刪除指定元素個數:"+jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("刪除下表0-3區間之外的元素:"+jedis.ltrim("collections", 0, 3));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(左端):"+jedis.lpop("collections"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections新增元素,從列表右端,與lpush相對應:"+jedis.rpush("collections", "EnumMap"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(右端):"+jedis.rpop("collections"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("修改collections指定下標1的內容:"+jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections的長度:"+jedis.llen("collections"));
System.out.println("獲取collections下標為2的元素:"+jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush("sortedList", "3","6","2","0","7","4");
System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序後:"+jedis.lrange("sortedList", 0, -1));
}
3.3 Set
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("============向集合中新增元素(不重複)============");
System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println("eleSet的所有元素為:"+jedis.smembers("eleSet"));
System.out.println("刪除一個元素e0:"+jedis.srem("eleSet", "e0"));
System.out.println("eleSet的所有元素為:"+jedis.smembers("eleSet"));
System.out.println("刪除兩個元素e7和e6:"+jedis.srem("eleSet", "e7","e6"));
System.out.println("eleSet的所有元素為:"+jedis.smembers("eleSet"));
System.out.println("隨機的移除集合中的一個元素:"+jedis.spop("eleSet"));
System.out.println("隨機的移除集合中的一個元素:"+jedis.spop("eleSet"));
System.out.println("eleSet的所有元素為:"+jedis.smembers("eleSet"));
System.out.println("eleSet中包含元素的個數:"+jedis.scard("eleSet"));
System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet", "e3"));
System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e1"));
System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e5"));
System.out.println("=================================");
System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8"));
//移到集合元素
System.out.println("將eleSet1中刪除e1並存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e1"));
System.out.println("將eleSet1中刪除e2並存入eleSet3中:"+jedis.smove("eleSet1", "eleSet3", "e2"));
System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
System.out.println("eleSet3中的元素:"+jedis.smembers("eleSet3"));
System.out.println("============集合運算=================");
System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
System.out.println("eleSet2中的元素:"+jedis.smembers("eleSet2"));
System.out.println("eleSet1和eleSet2的交集:"+jedis.sinter("eleSet1","eleSet2"));
System.out.println("eleSet1和eleSet2的並集:"+jedis.sunion("eleSet1","eleSet2"));
System.out.println("eleSet1和eleSet2的差集:"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1中有,eleSet2中沒有
jedis.sinterstore("eleSet4","eleSet1","eleSet2");//求交集並將交集儲存到dstkey的集合
System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4"));
}
3.4 Hash
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
Map<String,String> map = new HashMap<String,String>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
//新增名稱為hash(key)的hash元素
jedis.hmset("hash",map);
//向名稱為hash的hash中新增key為key5,value為value5元素
jedis.hset("hash", "key5", "value5");
System.out.println("雜湊hash的所有鍵值對為:"+jedis.hgetAll("hash"));//return Map<String,String>
System.out.println("雜湊hash的所有鍵為:"+jedis.hkeys("hash"));//return Set<String>
System.out.println("雜湊hash的所有值為:"+jedis.hvals("hash"));//return List<String>
System.out.println("將key6儲存的值加上一個整數,如果key6不存在則新增key6:"+jedis.hincrBy("hash", "key6", 6));
System.out.println("雜湊hash的所有鍵值對為:"+jedis.hgetAll("hash"));
System.out.println("將key6儲存的值加上一個整數,如果key6不存在則新增key6:"+jedis.hincrBy("hash", "key6", 3));
System.out.println("雜湊hash的所有鍵值對為:"+jedis.hgetAll("hash"));
System.out.println("刪除一個或者多個鍵值對:"+jedis.hdel("hash", "key2"));
System.out.println("雜湊hash的所有鍵值對為:"+jedis.hgetAll("hash"));
System.out.println("雜湊hash中鍵值對的個數:"+jedis.hlen("hash"));
System.out.println("判斷hash中是否存在key2:"+jedis.hexists("hash","key2"));
System.out.println("判斷hash中是否存在key3:"+jedis.hexists("hash","key3"));
System.out.println("獲取hash中的值:"+jedis.hmget("hash","key3"));
System.out.println("獲取hash中的值:"+jedis.hmget("hash","key3","key4"));
}
3.5 Key
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
System.out.println("清空資料:"+jedis.flushDB());
System.out.println("判斷某個鍵是否存在:"+jedis.exists("username"));
System.out.println("新增<'username','kuangshen'>的鍵值對:"+jedis.set("username", "kuangshen"));
System.out.println("新增<'password','password'>的鍵值對:"+jedis.set("password", "password"));
System.out.print("系統中所有的鍵如下:");
Set<String> keys = jedis.keys("*");
System.out.println(keys);
System.out.println("刪除鍵password:"+jedis.del("password"));
System.out.println("判斷鍵password是否存在:"+jedis.exists("password"));
System.out.println("檢視鍵username所儲存的值的型別:"+jedis.type("username"));
System.out.println("隨機返回key空間的一個:"+jedis.randomKey());
System.out.println("重新命名key:"+jedis.rename("username","name"));
System.out.println("取出改後的name:"+jedis.get("name"));
System.out.println("按索引查詢:"+jedis.select(0));
System.out.println("刪除當前選擇資料庫中的所有key:"+jedis.flushDB());
System.out.println("返回當前資料庫中key的數目:"+jedis.dbSize());
System.out.println("刪除所有資料庫中的所有key:"+jedis.flushAll());
}
3.6 Password
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
//驗證密碼,如果沒有設定密碼這段程式碼省略
//jedis.auth("password");
jedis.connect(); //連線
jedis.disconnect(); //斷開連線
jedis.flushAll(); //清空所有的key
}
4、通過Jedis再次理解 事務
public static void main(String[] args) {
//建立客戶端連線服務端,redis服務端需要被開啟
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello", "world");
jsonObject.put("name", "java");
//開啟事務
Transaction multi = jedis.multi();
String result = jsonObject.toJSONString();
try{
//向redis存入一條資料
multi.set("json", result);
//再存入一條資料
multi.set("json2", result);
//故意引發ArithmeticException: / by zero異常
int i = 100/0;
//如果沒有引發異常,執行進入佇列的命令
multi.exec();
}catch(Exception e){
e.printStackTrace();
//如果出現異常,回滾
multi.discard();
}finally{
System.out.println(jedis.get("json"));
System.out.println(jedis.get("json2"));
//最終關閉客戶端
jedis.close();
}
}
結果:由於異常,事務回滾。json,json2沒有執行成功,返回null。
java.lang.ArithmeticException: / by zero
at com.kuang.multi.TestMulti.main(TestMulti.java:26)
null
null