思路
现在redis中查询是否有缓存的页面代码,如果有就直接返回redis中存储的包含页面数据的页面代码。如果redis中没有的话,就查询一次数据库,并生成包含页面数据的html代码,存储的redis数据库并返回给浏览器。
存储在redis中的html代码.png核心代码:
// 手动渲染带数据的模板页面
SpringWebContext springWebContext = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
html = thymeleafViewResolver.getTemplateEngine().process("goods_list", springWebContext);
Demo.java
package cn.sitcat.Controller;
import cn.sitcat.domain.MiaoshaUser;
import cn.sitcat.redis.GoodsKey;
import cn.sitcat.redis.RedisService;
import cn.sitcat.service.GoodsService;
import cn.sitcat.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* @author Hiseico
* @create 2018-08-09 下午5:47
* @desc 商品页面
**/
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
GoodsService goodsService;
@Autowired
RedisService redisService;
@Autowired
ThymeleafViewResolver thymeleafViewResolver;
@Autowired
ApplicationContext applicationContext;
@RequestMapping(value = "/to_list", produces = "text/html")
@ResponseBody
public String toList(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser user) {
if (user == null) {
return "login";
}
model.addAttribute("user", user);
/**
* 有缓存的情况
*/
//现查询页面及页面数据是否缓存到redis 取缓存
String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
if (!StringUtils.isEmpty(html)) {
return html;
}
/**
* 没缓存的情况
*/
//查询商品列表
List<GoodsVo> list = goodsService.goodsVoList();
model.addAttribute("goodsList", list);
// 当缓存中没有数据的时候 手动渲染
SpringWebContext springWebContext = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
html = thymeleafViewResolver.getTemplateEngine().process("goods_list", springWebContext);
if (!StringUtils.isEmpty(html)) {
redisService.set(GoodsKey.getGoodsList, "", html);
}
return html;
}
@RequestMapping(value = "/to_detail/{goodsId}", produces = "text/html") //返回html
@ResponseBody
public String detail(HttpServletResponse response, HttpServletRequest request, Model model, MiaoshaUser user, @PathVariable("goodsId") long goodsId) {
if (user == null) {
return "login";
}
model.addAttribute("user", user);
/**
* 有缓存的情况
*/
//现查询页面及页面数据是否缓存到redis 取缓存
String html = redisService.get(GoodsKey.getGoodsDetail, ""+goodsId, String.class);
if (!StringUtils.isEmpty(html)) {
return html;
}
/**
* 没缓存的情况
*/
GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
model.addAttribute("goods", goods);
//秒杀开始结束时间,getTime转换为毫秒
long startAt = goods.getStartDate().getTime();
long endAt = goods.getEndDate().getTime();
long now = System.currentTimeMillis();
int miaoshaStatus = 0;//还没开始为0 已经结束为2 进行中为1
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);
// 当缓存中没有数据的时候 手动渲染
SpringWebContext springWebContext = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", springWebContext);
if (!StringUtils.isEmpty(html)) {
redisService.set(GoodsKey.getGoodsDetail, ""+goodsId, html);
}
return html;
}
}
网友评论