正常情况下我们使用Springboot时在Controller中返回视图只需要 return "login" 就可以返回对应的Templates中的login.html页面,但其实每次访问都是我们的服务器将该页面的所有资源都发送给浏览器,对于用户经常访问并且页面一般不需要改动的页面我们可以将其放入我们的Redis中做缓存,这样可以提高我们服务器的效率提升用户的体验。
准备我这里前端模板用的是thymeleaf
pom.xml
commons-codec commons-codecorg.apache.commons commons-lang3org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-data-redisorg.apache.commons commons-pool2org.projectlombok lombokorg.springframework.boot spring-boot-starter-thymeleaf
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.shao.cache.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
前端资源login.html
登录
Controller中的DemoController类
package com.shao.cache.controller;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
@Controller
@RequestMapping("/demo")
public class DemoController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ThymeleafViewResolver thymeleafViewResolver;
@RequestMapping("test01")
public String test01(Model model){
model.addAttribute("username","赵六");
return "login";
}
@ResponseBody
@RequestMapping(value = "test02",produces = "text/html;charset=utf-8")
public String test02(Model model, HttpServletRequest request, HttpServletResponse response){
ValueOperations valueOperations = redisTemplate.opsForValue();
String html = (String) valueOperations.get("login.html");
if(StringUtils.isNotEmpty(html)){
return html;
}
String username = "张三";
model.addAttribute("username",username);
//如果为空,手动渲染,存入redis并返回
WebContext webContext = new WebContext(request,response, request.getServletContext(),request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("login",webContext);
valueOperations.set("login.html",html,1, TimeUnit.MINUTES);
return html;
}
}
测试说明:
test01接口是正常的访问返回视图
test02接口是将整个页面放入Redis中并且有效时间是1分钟
测试test01接口
测试test02接口
Redis中确实存入的是整个页面
查询对应的key存活时间 此处是还有54秒失效
注意Controller中要在对应的做页面缓存的接口中加入如下
@ResponseBody
@RequestMapping(value = "test02",produces = "text/html;charset=utf-8")
不然解析器无法识别返回的字符串
总结在接口中缓存页面的几个主要步骤
1.判断是否有该缓存
2.有直接返回
3.没有就存入缓存
Ⅰ手动渲染
Ⅱ存入缓存并设置失效时间
4.返回该缓存



