使用SpringBoot初始化項目,集成測試遠程連接的Redis服務,並記錄了五大基本數據類型,在jedis中的體現方式。 ...
前期準備
Linux虛擬機或者租用的雲伺服器:sudo安裝redis,或者docker載入redis鏡像。後者需要使用docker啟用redis容器。
配置好redis.conf文件。註意:一定要註釋
# bind 127.0.0.1
其他詳細配置可參考我另一篇文章,不過能想到集成測試redis,配置文件應該已經配置好了。
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
開放6379埠
初始化SpringBoot項目
- 使用Spring Initializr 初始化一個SpringBoot項目,可以勾選NoSQL下這兩項,或者後面自行添加pom依賴。(當然也可以初始化一個maven項目來測試)
-
添加jedis依賴
<!--導入jedis依賴--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>
-
編寫簡易的單元測試程式
可使用
ifconfig
或者ip addr
查詢伺服器的ip地址/** * host: 虛擬機或遠程伺服器的ip地址 * port: redis.conf中配置的服務埠號 */ @Test void TestPing() { Jedis jedis = new Jedis("192.168.137.132",6379); System.out.println("TestPing >>>>>>>>>>>>> "+jedis.ping()); }
測試結果:
測試五大基本數據類型
測試過程中,有些數據防止重覆插入,部分測試用例使用了
del
命令。
-
五大基本數據類型:字元串,列表,哈希,集合,有序集合
-
每次單元測試都需進行遠程連接,故做以下處理:
@SpringBootTest class JedisDemoApplicationTests { // 此處配置連接 Jedis jedis = new Jedis("192.168.137.132",6379); @Test void contextLoads() { } // 此處進行單元測試 }
Key鍵
測試用例:
@Test
void KeyTest() {
System.out.println(jedis.keys("*"));
System.out.println(jedis.keys("*").getClass());
}
測試結果:
[list01, customer, set01, username]
class java.util.HashSet
String字元串
測試用例:
@Test
void StringTest() {
System.out.println("測試set >>> "+jedis.set("name", "kris"));
System.out.println("測試get >>> "+jedis.get("name"));
System.out.println(jedis.get("name").getClass());
System.out.println("\n測試append >>> "+jedis.append("name", " ^v^"));
System.out.println("測試strlen >>> "+jedis.strlen("name"));
System.out.println("測試get >>> "+jedis.get("name"));
System.out.println("\n測試mset >>> "+jedis.mset(
"username", "kris",
"age", "18",
"sex","男"));
System.out.println("測試mget >>> "+jedis.mget("username", "age", "sex"));
System.out.println("\n測試incr >>> "+jedis.incr("age"));
System.out.println("測試incrBy >>> "+jedis.incrBy("age",3));
System.out.println("測試decr >>> "+jedis.decr("age"));
System.out.println("測試decrBy >>> "+jedis.decrBy("age", 3));
}
測試結果:
測試set >>> OK
測試get >>> kris
class java.lang.String
測試append >>> 8
測試strlen >>> 8
測試get >>> kris ^v^
測試mset >>> OK
測試mget >>> [kris, 18, 男]
測試incr >>> 19
測試incrBy >>> 22
測試decr >>> 21
測試decrBy >>> 18
List列表
測試用例:
@Test
void ListTest() {
System.out.println("測試rpush >>> "+jedis.rpush("list01", "I", "love", "redis", "!"));
System.out.println("測試lpush >>> "+jedis.lpush("list01", "hello"));
System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1));
System.out.println(jedis.lrange("list01", 0, -1).getClass());
System.out.println("\n測試lpop >>> "+jedis.lpop("list01"));
System.out.println("測試rpop >>> "+jedis.rpop("list01"));
System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1));
System.out.println("\n測試lset >>> "+jedis.lset("list01", 0, "We"));
System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1));
System.out.println("\n測試 linsert >>> "+jedis.linsert("list01", ListPosition.AFTER,
"redis", "!"));
System.out.println("測試lrange >>> "+jedis.lrange("list01", 0, -1));
jedis.del("list01");
}
測試結果:
測試rpush >>> 4
測試lpush >>> 5
測試lrange >>> [hello, I, love, redis, !]
class java.util.ArrayList
測試lpop >>> hello
測試rpop >>> !
測試lrange >>> [I, love, redis]
測試lset >>> OK
測試lrange >>> [We, love, redis]
測試 linsert >>> 4
測試lrange >>> [We, love, redis, !]
Hash哈希
測試用例:
@Test
void HashTest() {
Map<String, String> customer = new HashMap<>();
customer.put("username", "kris");
customer.put("password", "123456");
customer.put("email", "[email protected]");
System.out.println("測試hset >>> "+jedis.hset("customer", customer));
System.out.println("測試hget >>> "+jedis.hget("customer", "email"));
System.out.println("測試getAll >>> "+jedis.hgetAll("customer"));
System.out.println(jedis.hgetAll("customer").getClass());
}
測試結果:
測試hset >>> 0
測試hget >>> [email protected]
測試getAll >>> {name=kris, [email protected], password=123456, username=kris}
class java.util.HashMap
Set集合
測試用例:
@Test
void SetTest() {
System.out.println("測試sadd >>> "+jedis.sadd("names", "louis", "kris", "Jerry", "Jackson"));
System.out.println("測試smembers >>> "+jedis.smembers("names"));
System.out.println("測試scard >>> "+jedis.scard("names"));
System.out.println(jedis.smembers("names").getClass());
System.out.println("\n測試srandmember >>> "+jedis.srandmember("names", 3));
System.out.println("測試srandmember >>> "+jedis.srandmember("names", 3));
System.out.println("\n測試spop >>> "+jedis.spop("names", 2));
System.out.println("測試smembers >>> "+jedis.smembers("names"));
System.out.println("\n測試sadd >>> "+jedis.sadd("names0", "Jack", "louis", "kris"));
System.out.println("測試sunion >>> "+jedis.sunion("names", "names0"));
System.out.println("測試sdiff >>> "+jedis.sdiff("names", "names0"));
System.out.println("測試sinter >>> "+jedis.sinter("names", "names0"));
System.out.println("測試smembers >>> "+jedis.smembers("names"));
System.out.println("測試smembers >>> "+jedis.smembers("names0"));
jedis.del("names", "names0");
}
測試結果:
測試sadd >>> 4
測試smembers >>> [kris, Jerry, Jackson, louis]
測試scard >>> 4
class redis.clients.jedis.BinaryJedis$SetFromList
測試srandmember >>> [kris, Jerry, Jackson]
測試srandmember >>> [Jerry, louis, Jackson]
測試spop >>> [Jerry, kris]
測試smembers >>> [Jackson, louis]
測試sadd >>> 3
測試sunion >>> [Jack, kris, louis, Jackson]
測試sdiff >>> [Jackson]
測試sinter >>> [louis]
測試smembers >>> [Jackson, louis]
測試smembers >>> [kris, Jack, louis]
ZSet有序集合
測試用例:
@Test
void SortSetTest() {
Map<String, Double> score = new HashMap<>();
score.put("鑽石", 1000.0);
score.put("王者", 2000.0); // 無序
score.put("星耀", 1500.0);
score.put("榮耀", 2500.0);
System.out.println("測試zadd >>> "+jedis.zadd("score", score));
System.out.println("測試zrangeByScore >>> "+jedis.zrangeByScore("score", 1500,2500));
System.out.println("測試zrange >>> "+jedis.zrange("score", 0,-1));
System.out.println("測試zrevrange >>> "+jedis.zrevrange("score", 0,-1));
System.out.println(jedis.smembers("names").getClass());
jedis.del("score");
}
測試結果:
測試zadd >>> 4
測試zrangeByScore >>> [星耀, 王者, 榮耀]
測試zrange >>> [鑽石, 星耀, 王者, 榮耀]
測試zrevrange >>> [榮耀, 王者, 星耀, 鑽石]
class redis.clients.jedis.BinaryJedis$SetFromList