1.页面缓存+URL缓存
页面缓存 与URL缓存 区别: URL缓存是指当进行redis缓存时,在页面缓存基础上加入了路径上的参数(goodsId)
package com.ryan.miaosha.controller;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import com.alibaba.druid.util.StringUtils;
import com.ryan.miaosha.dao.MiaoshaUser;
import com.ryan.miaosha.domain.Goods;
import com.ryan.miaosha.redis.BasePrefix;
import com.ryan.miaosha.redis.GoodsKey;
import com.ryan.miaosha.redis.RedisService;
import com.ryan.miaosha.service.GoodsService;
import com.ryan.miaosha.service.MiaoshaService;
import com.ryan.miaosha.vo.GoodsVo;
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
RedisService redisService;
@Autowired
GoodsService goodsService;
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
@RequestMapping(value="/to_list",produces="text/html")
@ResponseBody
String list(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user) {
model.addAttribute("user", user);
//判断redis缓存中是否存在商品列表html页面
String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
if(!StringUtils.isEmpty(html)) {
return html;
}
List<GoodsVo> goodsList=goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
//不存在html页面,那么我们自己手动渲染
WebContext wc=new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
html=thymeleafViewResolver.getTemplateEngine().process("goods_list", wc);
//判断手动渲染的html有没有,有则放入redis中,用于下一次使用
if(!StringUtils.isEmpty(html)) {
redisService.set(GoodsKey.getGoodsList, "", html);
}
return html;
/*
* GoodsKey 它的过期时间是60s,不能让缓存时间太长,不然用户一直看到的是同一个画面;1分钟刚刚好
*
* public class GoodsKey extends BasePrefix{
private GoodsKey(int expiredSeconds,String prefix) {
super(expiredSeconds,prefix);
}
public static GoodsKey getGoodsList=new GoodsKey(60,"gl");
}*/
}
@RequestMapping(value="/to_detail/{goodsId}",produces="text/html")
@ResponseBody
String detail(HttpServletRequest request,HttpServletResponse response,Model model,
MiaoshaUser user,@PathVariable("goodsId") long goodsId) {
model.addAttribute("user", user);
//判断redis缓存中是否存在商品列表html页面
//区别在于加了goodsId
String html = redisService.get(GoodsKey.getGoodsDetail, ""+goodsId, String.class);
if(!StringUtils.isEmpty(html)) {
return html;
}
GoodsVo goods=goodsService.getGoodsVoByGoodsId(goodsId);
model.addAttribute("goods", goods);
long startDate = goods.getStartDate().getTime();
long endDate = goods.getEndDate().getTime();
long currentTimeMillis = System.currentTimeMillis();
int miaoshaStatus=0;
int remainSeconds=0;
if(currentTimeMillis<startDate) {//秒杀还没开始 进入倒计时
miaoshaStatus=0;
remainSeconds=(int) ((startDate-currentTimeMillis)/1000);
} else if(currentTimeMillis>endDate) {//秒杀已经结束
miaoshaStatus=2;
remainSeconds=-1;
}else {//秒杀正在进行中
miaoshaStatus=1;
remainSeconds=0;
}
model.addAttribute("miaoshaStatus", miaoshaStatus);
model.addAttribute("remainSeconds", remainSeconds);
//不存在html页面,那么我们自己手动渲染
WebContext wc=new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
html=thymeleafViewResolver.getTemplateEngine().process("goods_detail", wc);
//判断手动渲染的html有没有,有则放入redis中,用于下一次使用
if(!StringUtils.isEmpty(html)) {
//区别在于加了goodsId
redisService.set(GoodsKey.getGoodsDetail, ""+goodsId, html);
}
return html;
}
}
网友评论