SpringBoot_Redis整合学习笔记

owofile Lv5

SpringBoot_Redis整合学习笔记

配置Redis

在SpringBoot项目中配置Redis

添加依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.0.4</version>
</dependency>
<!-- spring2.X集成redis所需common-pool2 如果上面引入的依赖是2.x则加-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>

修改配置文件

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
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/hospot?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
redis:
host: 127.0.0.1
port: 6379
database: 1
timeout: 1800000
# password: 123456
lettuce:
pool:
max-active: 50
max-wait: 100
max-idle: 25
min-idle: 20
# 启动事务管理
jpa:
generate-ddl: false
show-sql: true
properties:
hibernate:
format\_sql: true
transaction:
default-timeout: 60
rollback-on-commit-failure: true
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#mapper配置文件的位置
mapper-locations: classpath:mapper/xml/\*.xml
#别名
type-aliases-package: com.bdqn.student.entity
# 启用事务管理
transaction:
factory: managed

其中是在原有配置文件基础上增加了

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
redis:
host: 127.0.0.1
port: 6379
database: 1
timeout: 1800000
password: 123456
lettuce:
pool:
max-active: 20
max-wait: -1
max-idle: 5
min-idle: 0

在Config里添加RedisConfig.java

用来在SpringBoot项目中使用Redis,项目启动后自动配置文件

测试

在java测试类测试是否配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.springbootvue;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
@SpringBootTest
public class TextRedis {
@Resource
private RedisTemplate redisTemplate;
@Test
public void tRedis(){
redisTemplate.opsForValue().set("test","test");
}
}

使用

使用@Cacheable

1
2
3
4
5
6
7
8
@Cacheable(value = "userCache",key="#page.current+'\_'+#page.size")
@PostMapping("list")
public Page<Charts> findlist(@RequestBody Page<Charts> page) {
System.out.println("分页控制器就收参数:" + page);
Page<Charts> p = chartsService.page(page);
System.out.println(p);
return p;
}

通过使用@Cacheable注解

会将返回的值缓存到Redis数据库中

然后下次调用会在Redis中拿数据

其中Value是数据名 key是键值唯一 内容则是返回数

除了key值和value值,还有一个参数是unless

用来判断返回值是否为null,当返回结果没有则不缓存,这是为了实现防空判断,不然存储一个空值是没有意义的

示例:

1
@Cacheable(value="userCache",key = "#id",unless = "#result == null")

tip:查询时使用@Cacheable

使用@CacheEvict

示例:

1
@CacheEvict(value = "userCache",key = "#tSysUser.id",allEntries = true)

其中和上一个例子不同的是多了一个参数allEntries

它的作用是缓存时删掉之前的数据在增加新的数据

当你添加数据的时候,缓存也应该随之发生变化

tip:添加时使用@CacheEvict

使用@CachePut

使用@CachePut用来缓存更新数据

示例:

1
@CachePut(value = "userCache",key = "#id",allEntries = true,unless = "#result == null")

与增加不同的是,更新的注释不会删除之前的数据,而是在此基础上更新数据

  • Title: SpringBoot_Redis整合学习笔记
  • Author: owofile
  • Created at : 2023-12-26 01:43:04
  • Updated at : 2025-04-12 14:20:46
  • Link: https://owofile.github.io/blog/2023/12/26/SpringBoot-Redis整合学习笔记/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments