抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

摘要:无。

环境

Windows 10 企业版 LTSC 21H2
Redis 7.4.8
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18

1 前期准备

1.1 创建项目

目录结构:

code
1
2
3
4
5
6
7
8
9
10
11
demo-spring-data-redis/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── application.yml
│ └── test/
└── pom.xml

1.2 管理依赖

创建pom.xml文件:

pom.xml
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
43
44
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.example</groupId>
<artifactId>demo-spring-data-redis</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Spring Data Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

1.3 配置文件

创建application.yml文件:

application.yml
1
2
3
4
5
6
spring:
redis:
host: localhost
port: 6379
# 连接超时时间
timeout: 2000ms

2 基于Template使用

2.1 入门案例

Spring Data Redis提供了两种操作方式:

  • RedisTemplate:通用模板,默认使用JDK序列化,键值在Redis中不可读。
  • StringRedisTemplate:键值都是String类型,使用String序列化,在Redis中可读,最常用。

在启动类中实现CommandLineRunner接口,在应用启动后测试:

java
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
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private StringRedisTemplate stringRedisTemplate;

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Override
public void run(String... args) {
// 字符串操作
stringRedisTemplate.opsForValue().set("name", "张三");
stringRedisTemplate.opsForValue().set("code", "123456", 60, TimeUnit.SECONDS);
System.out.println("查询字符串:" + stringRedisTemplate.opsForValue().get("name"));
// 哈希操作
Map<String, String> userMap = new HashMap<>();
userMap.put("name", "张三");
userMap.put("age", "18");
stringRedisTemplate.opsForHash().putAll("user:1", userMap);
System.out.println("查询哈希:" + stringRedisTemplate.opsForHash().entries("user:1"));
// 列表操作
stringRedisTemplate.opsForList().rightPush("list", "a");
stringRedisTemplate.opsForList().rightPush("list", "b");
System.out.println("查询列表:" + stringRedisTemplate.opsForList().range("list", 0, -1));
// 集合操作
stringRedisTemplate.opsForSet().add("set", "a", "b");
System.out.println("查询集合:" + stringRedisTemplate.opsForSet().members("set"));
// 有序集合操作
stringRedisTemplate.opsForZSet().add("zset", "张三", 90);
stringRedisTemplate.opsForZSet().add("zset", "李四", 80);
System.out.println("查询有序集合:" + stringRedisTemplate.opsForZSet().rangeWithScores("zset", 0, -1));
// 键操作
System.out.println("是否存在键:" + stringRedisTemplate.hasKey("name"));
System.out.println("剩余过期时间:" + stringRedisTemplate.getExpire("code", TimeUnit.SECONDS));
stringRedisTemplate.delete("name");
}
}

2.2 自定义序列化

使用RedisTemplate存取对象时,需要配置序列化方式,否则键值都是JDK序列化的乱码:

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
StringRedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
}

使用RedisTemplate代替StringRedisTemplate存取对象即可。

评论