电商秒杀优化

电商秒杀学习之路之优化

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值