【原始代码】
@RequestMapping(value="/to_list", produces="text/html") @ResponseBody public String list(HttpServletRequest request, HttpServletResponse response, Model model, BaseUser user) { System.out.println("to_list 入参,用户信息:" + JSONObject.toJSONString(user)); model.addAttribute("user", user); // 商品列表 List<GoodsVo> goodsList = goodsService.goodsVoList(); model.addAttribute("goodsList", goodsList); return "goods_list"; }
【优化后代码】
@RequestMapping(value="/to_list", produces="text/html") @ResponseBody public String goodsList(HttpServletRequest request, HttpServletResponse response, Model model, BaseUser user) { System.out.println("to_list 入参,用户信息:" + JSONObject.toJSONString(user)); model.addAttribute("user", user); // 商品列表 List<GoodsVo> goodsList = goodsService.goodsVoList(); model.addAttribute("goodsList", goodsList); /** * 页面缓存处理 - 先查询缓存,确认缓存中是否有页面缓存 */ String html = redisService.get("goods_list", String.class); if(!StringUtils.isEmpty(html)) { return html; } // 如果缓存中没有页面,则开始手动渲染页面 SpringWebContext ctx = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext ); //手动渲染 html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx); System.out.println("手动渲染得到的模板html = " + html); if(!StringUtils.isEmpty(html)) { redisService.set(null,"goods_list",html,60); // 缓存一分钟 } return html; }