thymeleaf页面缓存

一般做法是controller中搞个model,然后返回html,于是model与html渲染在一起呈现给用户。

现做法逻辑:

1.先去redis中找有无该页面。
2.无的话,还是一样,先把数据搞到model中。但这里要自己把model和html结合,渲染出一个html出来
3.把自己渲染的html装到redis中,设置时间为1min。
4.返回该html。

package com.imooc.miaosha.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.redis.GoodsKey;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.MiaoshaUserService;
import com.imooc.miaosha.vo.GoodsDetailVo;
import com.imooc.miaosha.vo.GoodsVo;

@Controller
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	MiaoshaUserService userService;
	
	@Autowired
	RedisService redisService;
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;
	
	@Autowired
	ApplicationContext applicationContext;
	
	/**
	 * QPS:1267 load:15 mysql
	 * 5000 * 10
	 * QPS:2884, load:5 
	 * */
    @RequestMapping(value="/to_list", produces="text/html")
    @ResponseBody
    public String list(HttpServletRequest request, HttpServletResponse response, Model model,MiaoshaUser user) {
    	model.addAttribute("user", user);
    	//取缓存
    	String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
    	if(!StringUtils.isEmpty(html)) {
    		return html;
    	}
    	List<GoodsVo> goodsList = goodsService.listGoodsVo();
    	model.addAttribute("goodsList", goodsList);
//    	 return "goods_list";
    	SpringWebContext ctx = new SpringWebContext(request,response,
    			request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
    	//手动渲染
    	html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
    	if(!StringUtils.isEmpty(html)) {
    		redisService.set(GoodsKey.getGoodsList, "", html);
    	}
    	return html;
    }
    
}

解释:

注意点1:

@RequestMapping(value="/to_list", produces="text/html")
		@ResponseBody

products意思为返回的是何种类型的资源(这里是text类html),ResponseBody有两重意思,一是将controller中return的东西写入ResponseBody中,二是指定了格式为json。所以这里这两个注解搭配,指的是在responseBody中写入html的源代码。

注意点2:

SpringWebContext ctx = new SpringWebContext(request,response,
					request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
			//手动渲染
html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);

这是手动渲染的整个过程:这里的html就是与model组装好了的html
其中:

		@Autowired
		ThymeleafViewResolver thymeleafViewResolver;

		@Autowired
		ApplicationContext applicationContext;

这两个类都是autowire过来的。

发布了56 篇原创文章 · 获赞 1 · 访问量 1509

猜你喜欢

转载自blog.csdn.net/weixin_44841849/article/details/105195642
今日推荐