Redis(五 )Jedis的使用
2022-09-22 22:46:44

目录
[TOC]

Jedis是什么?

Jedis是Redis官方推荐的Java连接开发工具。也就是说可以像在redis-cli写命令一样来操作redis,只不过在Java中变成了调用方法。而这些方法jedis都封装好了

Jedis如何使用?

首先在IDEA创建一个maven项目

引入Pom

1
2
3
4
5
6
7
8
9
10
11
12
<!--导入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>

测试连接

首先要运行redis-server
image.png
然后创建一个TestPing.java文件

TestPing.java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package top.hopestation;
import redis.clients.jedis.Jedis;

public class TestPing {
public static void main(String[] args) {
//1. new Jedis对象
Jedis jedis = new Jedis("127.0.0.1",6379);
//Jedis 所有的命令就是 之前学习的命令
System.out.println(jedis.ping());
}
}
//运行结果如下:
//-------
PONG //说明了连接redis
Process finished with exit code 0
//-------

测试命令

这里的jedis的方法,就是对应着redis-cli(客户端)的一个一个命令。
image.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package top.hopestation;

import redis.clients.jedis.Jedis;

public class TestPing {
public static void main(String[] args) {
//1. new Jedis对象
Jedis jedis = new Jedis("127.0.0.1",6379);
//Jedis 所有的命令就是 之前学习的命令
System.out.println(jedis.ping());
System.out.println("判断某个键是否存在==>" + jedis.exists("name"));
System.out.println("新增测试1==>" + jedis.set("name","hope"));
System.out.println("新增测试2==>" + jedis.set("age","15"));
System.out.println("所有的键==>" + jedis.keys("*"));
System.out.println("删除键" + jedis.del("name"));
System.out.println("判断某个键是否存在==>" + jedis.exists("name"));
System.out.println("新增测试3==>" + jedis.set("message","ceshi"));
System.out.println("新增测试4==>" + jedis.set("k1","v1"));
System.out.println("获取一个随机key==>" + jedis.randomKey());
System.out.println("判断某个键是否存在==>" + jedis.rename("k1","newk1"));
System.out.println("获取改后的key==>" + jedis.get("newk1"));
System.out.println("返回当前数据库的key的数目==>" + jedis.dbSize());
}
}
//运行结果如下:
//-------
PONG
判断某个键是否存在==>false
新增测试1==>OK
新增测试2==>OK
所有的键==>[name, key:__rand_int__, counter:__rand_int__, money, age, mylist, out]
删除键1
判断某个键是否存在==>false
新增测试3==>OK
新增测试4==>OK
获取一个随机key==>counter:__rand_int__
判断某个键是否存在==>OK
获取改后的key==>v1
返回当前数据库的key的数目==>8

Process finished with exit code 0
//-------

测试事务

正常情况

事务会进行下去,并从结果能get到,事务操作的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package top.hopestation;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class TestTransaction {
public static void main(String[] args) {
//1. new Jedis对象
Jedis jedis = new Jedis("127.0.0.1",6379);
//新建一个JSON字符串
JSONObject jsonObject = new JSONObject();
jsonObject.put("helo","world");
jsonObject.put("name","hopestation");
System.out.println(jedis.ping());
//这里还可以加上jedis.watch("...")
Transaction tran = jedis.multi();
try {
String jsonStr = jsonObject.toJSONString();
tran.set("user1",jsonStr);
tran.set("user2",jsonStr);
tran.exec();
} catch (Exception e) {
tran.discard();
e.printStackTrace();
}finally {
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}
}
}
//结果
//-------
PONG
{"helo":"world","name":"hopestation"}
{"helo":"world","name":"hopestation"}

Process finished with exit code 0
//-------

出错情况

下面我们故意在EXEC前,让事务报错,那么会在catch
里执行DISCARD事务会被丢弃,所以结果中获取不到值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   try {
String jsonStr = jsonObject.toJSONString();
tran.set("user3",jsonStr);
tran.set("user4",jsonStr);
//故意在EXEC前,让事务报错
int i = 1/0;
tran.exec();
} catch (Exception e) {
tran.discard();
e.printStackTrace();
}finally {
System.out.println(jedis.get("user3"));
System.out.println(jedis.get("user4"));
jedis.close();
}
//结果
//-------
PONG
java.lang.ArithmeticException: / by zero
at top.hopestation.TestTransaction.main(TestTransaction.java:21)
null
null

Process finished with exit code 0

//-------
Prev
2022-09-22 22:46:44
Next