电商秒杀优化

电商秒杀学习之路之优化

1.页面优化介绍

页面缓存+URL缓存+对象缓存

增加并行数量,就是增大对数据库的访问。而这三种优化缓存效果排序:页面缓存>URL缓存>对象缓存

页面静态化,前后端分离

html浏览器,提前将任务在后台中缓存下来,无需重复下载,只需下载动态数据就可以了

静态资源优化

图片,jss,js这些内容的优化

CDN优化

2.开始操作

首先在GoodsController中找到商品列表goodlist,数据通过model来传到good_list.html页面中去电商秒杀优化

 

那么如何取出我们的页面缓存呢?

通过下面这句代码


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;
    }

以往我们的渲染一直通过springboot中的thymelaf来辅助我们渲染,现在我们通过自己手动渲染


//手动渲染
    html = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
    if (!StringUtils.isEmpty(html)) {
        redisService.set(GoodsKey.getGoodsList, "", html);
    }
    return html;
}

process的两个参数分别是(模板名称,业务数据context),其中创建context包含很多参数,如下:


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

到这为止就是我们的页面缓存