Jedis基础详解
Jedis
使用Java來操作Redis
什么是Jedis 是Redis官方推薦的Java操作Redis中間件, 如果你要使用Java操作Redis, 那么就該對jedis熟悉
測試
- 導入對應的依賴
- 編碼測試
- 連接數據庫
- 操作命令
- 斷開連接
PING
@Testvoid TestPing() {//1 new Jedis 對象即可Jedis jedis = new Jedis("127.0.0.1", 6379);//jedis 所有的命令就是他的基本命令, 就是對象的方法System.out.println(jedis.ping());;}輸出PONG
常用API
- String
- List
- Set
- Hash
- Zset
Redis-key
@Testvoid TestKey() {Jedis jedis = new Jedis("127.0.0.1", 6379);System.out.println("清空數據" + jedis.flushDB());System.out.println("判斷某個鍵是否存在: " + jedis.exists("username"));System.out.println("新增'<username, mxz>的鍵值對'" + jedis.set("username", "mxz"));System.out.println("新增'<password, mxz>的鍵值對'" + jedis.set("password", "mxz"));System.out.println("系統中所有的鍵值對如下: ");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("按索引查詢: " + jedis.select(0));System.out.println("刪除當前選擇數據庫的所有的key: " + jedis.flushDB());System.out.println("返回當前數據庫中key的數目: " + jedis.dbSize());System.out.println("刪除所有數據庫中的所有的key: " + jedis.flushAll());}輸出
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.4.1)2021-01-08 19:28:31.602 INFO 7492 --- [ main] cn.com.codincge.RedisApplicationTests : Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 7492 (started by mxz in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-08 19:28:31.608 INFO 7492 --- [ main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: default 2021-01-08 19:28:32.290 INFO 7492 --- [ main] cn.com.codincge.RedisApplicationTests : Started RedisApplicationTests in 1.133 seconds (JVM running for 3.804)清空數據OK 判斷某個鍵是否存在: false 新增'<username, mxz>的鍵值對'OK 新增'<password, mxz>的鍵值對'OK 系統中所有的鍵值對如下: [password, username] 刪除鍵password: 1 判斷鍵password是否存在: false 查看鍵username所存儲值的類型: string 隨機返回key空間的一個: username 重命名key: OK 按索引查詢: OK 刪除當前選擇數據庫的所有的key: OK 返回當前數據庫中key的數目: 0 刪除所有數據庫中的所有的key: OKString(字符串)
@Testvoid TestString() {Jedis jedis = new Jedis("127.0.0.1", 6379);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", "valueChanged"));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"));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));}輸出
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.4.1)2021-01-09 09:56:23.763 INFO 9088 --- [ main] cn.com.codincge.RedisApplicationTests : Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 9088 (started by mxz in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 09:56:23.825 INFO 9088 --- [ main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: default 2021-01-09 09:56:25.462 INFO 9088 --- [ main] cn.com.codincge.RedisApplicationTests : Started RedisApplicationTests in 2.349 seconds (JVM running for 5.252)=====增加數據===== OK OK OK 刪除鍵key21 獲取鍵key2null 修改key1OK 獲取key1的值valueChanged 在key3后面加入值9 key3的值value3end 增加多個鍵值對: OK 獲取多個鍵值對: [value01, value02, null] 獲取多個鍵值對: [value01, value02, null, null] 刪除多個鍵值對: 2 獲取多個鍵值對: [null, null, null] =====新增鍵值對防止覆蓋原先值===== 1 1 0 value1 value2 =====新增鍵值對并設置有效時間===== OK value3 null =====獲取原值, 更新為新值===== value2 key2GetSet 獲得key2的值字符串: y2GList(列表)
@Testvoid TestList() {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代表倒數System.out.println("collections區間 0-3 的元素: " + jedis.lrange("collections", 2, 3));System.out.println("===========================================");// 刪除列表指定的值, 第二個參數為刪除的個數(有重復時), 后add進去的值先被刪, 類似于出棧System.out.println("刪除指定元素個數: " + jedis.lrem("collectionws", 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", "test"));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, "FLinkedHashMap"));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", "4", "5", "7", "9");System.out.println("sortedList排序前: " + jedis.lrange("sortedList", 0, -1));System.out.println(jedis.sort("sortedList"));System.out.println("sortedList排序后:" + jedis.lrange("sortedList", 0, -1));}輸出
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.4.1)2021-01-09 10:43:37.900 INFO 12624 --- [ main] cn.com.codincge.RedisApplicationTests : Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 12624 (started by mxz in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 10:43:37.900 INFO 12624 --- [ main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: default 2021-01-09 10:43:38.572 INFO 12624 --- [ main] cn.com.codincge.RedisApplicationTests : Started RedisApplicationTests in 0.997 seconds (JVM running for 2.26)=====添加一個List===== collections的內容[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList] collections區間 0-3 的元素: [HashSet, LinkedHashMap] =========================================== 刪除指定元素個數: 0 collections的內容: [TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList] 刪除下表0-3區間之外的元素: OK collections內容: [TreeMap, TreeSet, HashSet, LinkedHashMap] collections列表出棧(左端): TreeMap collections的內容: [TreeSet, HashSet, LinkedHashMap] collections添加元素, 從列表右端, 與lpush相對應: 4 collections的內容: [TreeSet, HashSet, LinkedHashMap, test] collections列表出棧(右端): test collections的內容: [TreeSet, HashSet, LinkedHashMap] collections指定下標 1 的內容: OK collections的內容: [TreeSet, FLinkedHashMap, LinkedHashMap] =========================================== collections的長度: 3 獲取collections下標為 2 的元素LinkedHashMap =========================================== sortedList排序前: [9, 7, 5, 4, 2, 6, 3] [2, 3, 4, 5, 6, 7, 9] sortedList排序后:[9, 7, 5, 4, 2, 6, 3]Set(集合)
@Testvoid TestSet() {Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.flushDB();System.out.println("======向集合中添加元素(不重復)======");System.out.println(jedis.sadd("eleSet", "e0", "e1", "e2", "e3", "e4", "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("刪除兩個元素e6 e7" + 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("e5是否在eleSet中: " + jedis.sismember("eleSet", "e5"));System.out.println("========================================");System.out.println(jedis.sadd("eleSet1", "e0", "e1", "e2", "e3", "e4", "e5"));System.out.println(jedis.sadd("eleSet2", "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8"));System.out.println("將eleSet1中刪除e1并存入eleSet3中: " + jedis.smove("eleSet1", "eleSet3", "e1"));System.out.println("將eleSet2中刪除e2并存入eleSet3中: " + jedis.smove("eleSet2", "eleSet3", "e2"));System.out.println("eleSet1中的元素: " + jedis.smembers("eleSet1"));System.out.println("eleSet2中的元素: " + jedis.smembers("eleSet2"));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"));}輸出
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.4.1)2021-01-09 11:25:15.843 INFO 6516 --- [ main] cn.com.codincge.RedisApplicationTests : Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 6516 (started by mxz in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 11:25:15.843 INFO 6516 --- [ main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: default 2021-01-09 11:25:16.718 INFO 6516 --- [ main] cn.com.codincge.RedisApplicationTests : Started RedisApplicationTests in 1.207 seconds (JVM running for 2.739)======向集合中添加元素(不重復)====== 6 1 0 eleSet的所有元素為: [e5, e1, e0, e2, e3, e6, e4] 刪除一個元素e01 刪除兩個元素e6 e71 eleSet的所有元素為: [e5, e1, e2, e3, e4] 隨機移除集合中的一個元素: e5 隨機移除集合中的一個元素: e2 eleSet的所有元素為: [e1, e3, e4] eleSet中包含元素的個數: 3 e3是否在eleSet中: true e1是否在eleSet中: true e5是否在eleSet中: false ======================================== 6 9 將eleSet1中刪除e1并存入eleSet3中: 1 將eleSet2中刪除e2并存入eleSet3中: 1 eleSet1中的元素: [e3, e5, e0, e2, e4] eleSet2中的元素: [e1, e0, e3, e6, e4, e8, e5, e7] =====集合運算===== eleSet1中的元素: [e3, e5, e0, e2, e4] eleSet2中的元素: [e1, e0, e3, e6, e4, e8, e5, e7] eleSet1和eleSet2的交集[e3, e5, e0, e4] eleSet1和eleSet2的并集[e7, e5, e1, e0, e8, e2, e3, e4, e6] eleSet1和eleSet2的差集[e2] eleSet4中的元素: [e0, e5, e4, e3]Hash(哈希)
@Testvoid TestHash() {Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.flushDB();Map<String, String> map = new HashMap<>();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"));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", 1));System.out.println("散列hash的所有鍵值對為: " + jedis.hgetAll("hash"));System.out.println("將key6保存的值加上一個整數, 如果key6不存在則添加key6: " + jedis.hincrByFloat("hash", "key6", 1.0));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"));}輸出
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.4.1)2021-01-09 13:27:34.237 INFO 11788 --- [ main] cn.com.codincge.RedisApplicationTests : Starting RedisApplicationTests using Java 1.8.0_181 on DESKTOP-GMI8GKQ with PID 11788 (started by mxz in D:\mxz_code\codingce-java\codingce-redis\redis-01-jedis) 2021-01-09 13:27:34.242 INFO 11788 --- [ main] cn.com.codincge.RedisApplicationTests : No active profile set, falling back to default profiles: default 2021-01-09 13:27:35.059 INFO 11788 --- [ main] cn.com.codincge.RedisApplicationTests : Started RedisApplicationTests in 1.276 seconds (JVM running for 3.17)散列hash的所有鍵值對為: {key1=value1, key2=value2, key5=value5, key3=value3, key4=value4} 散列hash的所有的鍵為: [key1, key2, key5, key3, key4] 散列hash的所有的值為: [value1, value3, value4, value2, value5] 將key6保存的值加上一個整數, 如果key6不存在則添加key6: 1 散列hash的所有鍵值對為: {key1=value1, key2=value2, key5=value5, key6=1, key3=value3, key4=value4} 將key6保存的值加上一個整數, 如果key6不存在則添加key6: 2.0 散列hash的所有鍵值對為: {key1=value1, key2=value2, key5=value5, key6=2, key3=value3, key4=value4} 刪除一個或多個鍵值對: 1 散列hash的所有鍵值對為: {key1=value1, key5=value5, key6=2, key3=value3, key4=value4} 散列hash的所有鍵值對個數: 5 判斷散列hash中是否存在key2: false 判斷散列hash中是否存在key3: true 獲取hash中的值: [value3] 獲取hash中的值: [value3, value4]項目地址 https://github.com/xzMhehe/codingce-java
總結
- 上一篇: github怎么删除已经发布的Relea
- 下一篇: 如何关闭idea中反编译文件时的弹框提示