瓶颈在数据库。
解决办法加缓存。
缓存:
1.用户在发送请求时使用页面的静态化,把页面缓存在客户端
2.请求还没有到达网站之前可以部署CDN节点,让请求首先访问CDN
3.可以使用Nginx缓存
4.接着在使用页面级缓存在详细点可以使用对象缓存
1.页面缓存+URL缓存+对象缓存
1.取缓存
2.手动渲染模版
3.结果输出
/**
* 使用页面缓存技术
*
* @param model
* @param seckillUser
* @return
* @throws Exception
**/
@GetMapping("shop")
@ResponseBody
public String login(HttpServletRequest request, HttpServletResponse response, SeckillUser seckillUser, Model model) {
List<GoodsVo> byAll = iSeckillGoodsVoService.findByAll();
SeckillUser seckillUser1 = new SeckillUser();
seckillUser1.setId("1");
seckillUser1.setNickName("bill");
seckillUser1.setPhone("15733895935");
seckillUser1.setPassword("123456");
model.addAttribute("goodslist", byAll);
model.addAttribute("user", seckillUser1);
//取缓存
String html = redisUtil.get(GoodsKey.goodsKey, "", String.class);
if (!StringUtils.isEmpty(html)) {
return html;
}
WebContext webContext = new WebContext(request, response, request.getServletContext(),
request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("shop", webContext);
if (!StringUtils.isEmpty(html)) {
redisUtil.set(GoodsKey.goodsKey, "", html);
}
return html;
}
URL缓存技术 (不同的商品id 有不同缓存)
@GetMapping("to_detail/{seckillGoodsId}")
public String title(@PathVariable("seckillGoodsId") String seckillGoodsId, SeckillUser user, Model model,
HttpServletResponse response, HttpServletRequest request) throws ParseException {
model.addAttribute("user", user);
String html = redisUtil.get(GoodsKey.goodsKey, "" + seckillGoodsId, String.class);
if (!StringUtils.isEmpty(html)) {
return html;
}
GoodsVo goodsVo = iSeckillGoodsVoService.findByid(seckillGoodsId);
model.addAttribute("goods", goodsVo);
//开始时间
long startAt = DateUtils.formatString(goodsVo.getStartDate());
//结束时间
long endAt = DateUtils.formatString(goodsVo.getEndDate());
//现在时间
long now = System.currentTimeMillis();
int miaoshaStatus = 0;
int remainSeconds = 0;
if (now < startAt) {
miaoshaStatus = 0;
remainSeconds = (int) (startAt - now) / 1000;
} else if (now > endAt) {
miaoshaStatus = 2;
remainSeconds = -1;
} else {
miaoshaStatus = 1;
remainSeconds = 0;
}
model.addAttribute("miaoshaStatus", miaoshaStatus);
model.addAttribute("remainSeconds", remainSeconds);
WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("detail", webContext);
if (!StringUtils.isEmpty(html)) {
redisUtil.set(GoodsKey.goodsKey, "" + seckillGoodsId, html);
}
return html;
}
service 层优化
/**
* 查询全部商品信息
*
* @return
*/
@Override
public List<GoodsVo> findByAll() {
List<GoodsVo> goodsVo1 = redisUtil.getList(MiaoShaGoodsKey.miaoShaGoodsKey,"",GoodsVo.class);
if (goodsVo1 != null){
return goodsVo1;
}else {
ArrayList<GoodsVo> goodsVos = new ArrayList<>();
try {
List<SeckillGoods> seckillGoods = seckillGoodsRepository.findAll();
if (seckillGoods != null && !seckillGoods.isEmpty()) {
for (SeckillGoods seckillGood : seckillGoods) {
GoodsVo goodsVo = new GoodsVo();
Goods allById = goodsRepository.findById(seckillGood.getGoodsId()).get();
goodsVo.setStartDate(DateUtils.formatDate(seckillGood.getStartDate()));
goodsVo.setEndDate(DateUtils.formatDate(seckillGood.getEndDate()));
goodsVo.setStockCount(seckillGood.getStockCount());
goodsVo.setSeckillPrice(seckillGood.getSeckillPrice());
goodsVo.setGoodsName(allById.getGoodsName());
goodsVo.setGoodsTitle(allById.getGoodsTitle());
goodsVo.setGoodsImg(allById.getGoodsImg());
goodsVo.setGoodsDetail(allById.getGoodsDetail());
goodsVo.setGoodsPrice(allById.getGoodsPrice());
goodsVo.setGoodsStock(allById.getGoodsStock());
goodsVo.setGoodsId(allById.getId());
goodsVo.setSecckillGoodsId(seckillGood.getId());
goodsVos.add(goodsVo);
}
}
redisUtil.set(MiaoShaGoodsKey.miaoShaGoodsKey,"",goodsVos);
} catch (Exception e) {
log.error("出错了", e.getMessage());
return null;
}
return goodsVos;
}
}
将数据缓存至rides中,不必每次请求都查询mysql,明显提高了吞吐量