页面缓存和URL缓存
标签:项目总结

页面缓存和URL缓存

1. 页面缓存

主要思路是将html页面存在redis中,在一定时间内的访问可以直接从redis中获得页面

    @RequestMapping(value = "/to_list", produces = "text/html")
    @ResponseBody
    public String toList(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser miaoshaUser) {
        model.addAttribute("user", miaoshaUser);
        //        取缓存
        String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
        if (!StringUtils.isEmpty(html)) {
            log.info("{}", "list get from redis");
            return html;
        }
        List<GoodsVo> goodsList = goodsService.listGoodsVo();
        model.addAttribute("goodsList", goodsList);

//        手动渲染
        WebContext ctx = new WebContext(request, response, request.getServletContext(),
                request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
        if (!StringUtils.isEmpty(html)) {
            redisService.set(GoodsKey.getGoodsList, "", html);
        }
        log.info("{}", "list set to redis");
        return html;
    }

RequestMapping中添加 produces="text/html"

下面会先从redis中取一下,如果不空直接返回,否者利用thymeleafViewResolver生成页面,存到redis中并返回

注意上面的process方法中,会需要一个IContext参数,在Spring4中使用的是SpringWebContext

SpringWebContext ctx = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );

applicationContext可以通过@Autowired注入进来

在Spring5中使用的WebContext

WebContext ctx = new WebContext(request, response, request.getServletContext(),
                request.getLocale(), model.asMap());

2. URL缓存

对URL进行缓存就是不同的参数有不同的页面,比如商品详情就跟id相关

 @RequestMapping(value = "/to_detail/{goodsId}", produces = "text/html")
    @ResponseBody
    public String toDetail(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser miaoshaUser, @PathVariable("goodsId") long goodsId) {
        model.addAttribute("user", miaoshaUser);
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods", goods);
        //        取缓存
        String html = redisService.get(GoodsKey.getGoodsDetail, "" + goodsId, String.class);
        if (!StringUtils.isEmpty(html)) {
            log.info("{}", "detail get from redis");
            return html;
        }

        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int miaoshaStatus = 0;
        int remainSeconds = 0;
        if (now < startAt) {
            miaoshaStatus = 0;
            remainSeconds = (int) ((endAt - now) / 1000);
        } else if (now > endAt) {
            miaoshaStatus = 2;
            remainSeconds = -1;
        } else {
            miaoshaStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("miaoshaStatus", miaoshaStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        //        手动渲染
        WebContext ctx = new WebContext(request, response, request.getServletContext(),
                request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", ctx);
        if (!StringUtils.isEmpty(html)) {
            redisService.set(GoodsKey.getGoodsDetail, "" + goodsId, html);
        }
        log.info("{}", "detail set to redis");
        return html;
    }

大部分跟上面的页面缓存一样,只是在设置缓存key的时候要将相关的id设置进去,这样取的时候就可以根据id进行区分。

  • 2 min read

CONTRIBUTORS


  • 2 min read