Java學習—java-Redis

vly521發表於2020-09-26

jedis
jedis是官方推薦的java開發連線工具,相當於java操作Redis的一箇中介軟體

開始操作:
1.建立一個空的maven專案
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
一個空的maven就建立完成了
2.匯入對應的maven依賴

<dependencies>
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>

    </dependencies>

3.編碼測試

  • 連線資料庫
  • 執行命令
    記得開啟 redis的服務端
public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1",6379);
        System.out.println(jedis.ping());
    }

下面開始測試
基本操作

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
    }

基本操作

package com.kuang.base;

import redis.clients.jedis.Jedis;

import java.util.Set;

public class TestKey {
    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());
    }
}

String

import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

public class TestString {
    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));
    }
}

list

package com.kuang.base;

import redis.clients.jedis.Jedis;

public class TestList {
    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));
    }
}

set

package com.kuang.base;

import redis.clients.jedis.Jedis;

public class TestSet {
    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"));
    }
}

hash

package com.kuang.base;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

public class TestHash {
    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"));
    }
}