redis

添加 POM 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>compile</scope>
</dependency>
</dependencies>

连接 Redis

通过 Jedis 连接 Redis 只需要 new 一个 Jedis 对象即可

通过 ping 命令测试与 Redis 的连接,返回 PONG 说明连接成功

1
2
3
4
5
6
7
8
9
10
11
/**
* @author hominsu
*/
public class TestPing {
public static void main(String[] args) {
// new Jedis 对象
var jedis = new Jedis("127.0.0.1", 6379);

System.out.println(jedis.ping());
}
}

输出:

1
2
3
PONG

进程已结束,退出代码为 0

常用 API

String

List

Set

Hash

Sorted Set