简述
正常情况下我们使用Springboot时在Controller中返回视图只需要 return "login" 就可以返回对应的Templates中的login.html页面,但其实每次访问都是我们的服务器将该页面的所有资源都发送给浏览器,对于用户经常访问并且页面一般不需要改动的页面我们可以将其放入我们的Redis中做缓存,这样可以提高我们服务器的效率提升用户的体验。
准备
我这里前端模板用的是thymeleaf
pom.xml
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
application.yaml
server:
port: 8080
#redis
spring:
redis:
#服务器地址
host: localhost
#端口
port: 6379
#数据库
database: 0
#超时时间
connect-timeout: 10000ms
lettuce:
pool:
#最大连接数,默认是8
max-active: 8
#最大连接阻塞等待时间 默认是-1
max-wait: 10000ms
#最大空闲连接,默认是8
max-idle: 200
#最小空闲连接,默认是0
min-idle: 5
config中RedisConfig类
package com.s