一、页面缓存
1、定义GoodsKey
package com.mydre.miaosha.redis;
public class GoodsKey extends BasePrefix{
private GoodsKey(int expireSeconds, String prefix){
super(expireSeconds, prefix);
}
public static GoodsKey getGoodsList = new GoodsKey(60, "gl");//设置页面缓存的时间是1分钟,在缓存的同时保持页面的更新
}
2、GoodsController
@RequestMapping(value="/to_list", produces="text/html")
@ResponseBody
public String list(Model model, MiaoshaUser user,HttpServletRequest request,HttpServletResponse response) {
List<GoodsVo> goodsList = goodsService.listGoodsVo();
model.addAttribute("goodsList", goodsList);
model.addAttribute("user", user);
//return "goods_list";
//从redis数据库中获取数据
String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
//取缓存
if(!StringUtils.isEmpty(html)){
return html;
}
//手动渲染
SpringWebContext springWebContext = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
html = thymeleafViewResolver.getTemplateEngine().process("goods_list", springWebContext);
redisService.set(GoodsKey.getGoodsList, "", html);
if(!StringUtils.isEmpty(html)){
return html;
}
return html;
}
3、查看缓存。在刷新将要被缓存的页面之后,理解查看redis数据库中的数据:
127.0.0.1:6379> keys GoodsKey:gl
1) "GoodsKey:gl"
127.0.0.1:6379> get GoodsKey:gl
"<!DOCTYPE html>\n\n<html><!-- \xe5\xbc\x95\xe5\x85\xa5thymeleaf \xe7\x9a\x84 \xe5\x91\xbd\xe5\x90\x8d\xe7\xa9\xba\xe9\x97\xb4\xef\xbc\x88th\xef\xbc\x89 -->\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\" />\n<title>\xe5\x95\x86\xe5\x93\x81\xe5\x88\x97\xe8\xa1\xa8</title>\n<!-- jquery -->\n<!-- \xe8\xbf\x99\xe9\x87\x8c / \xe4\xbb\xa3\xe8\xa1\xa8 \xe7\x9a\x84\xe6\x98\xafstatic\xe6\x96\x87\xe4\xbb\xb6\xe5\xa4\xb9\xef\xbc\x8c\xe8\xbf\x99\xe6\x98\xafthymeleaf\xe5\xbc\x95\xe5\x85\xa5\xe9\x9d\x99\xe6\x80\x81\xe6\x96\x87\xe4\xbb\xb6\xe7\x9a\x84\xe6\x96\xb9\xe5\xbc\x8f\xe3\x80\x82 -->\n<script type=\"text/javascript\" src=\"/js/jquery.min.js\"></script>\n<!-- bootstrap -->\n<link rel=\"stylesheet\" type=\"text/css\" href=\"/bootstrap/css/bootstrap.min.css\" />\n<script type=\"text/javascript\" src=\"/bootstrap/js/bootstrap.min.js\"></script>\n<!-- jquery-validation -->\n<script type=\"text/javascript\" src=\"/jquery-validation/jquery.validate.min.js\"></script>\n<script type=\"text/javascript\" src=\"/jquery-validation/localization/messages_zh.js\"></script>\n<!-- layer -->\n<script type=\"text/javascript\" src=\"/layer/layer.js\"></script>\n</head>\n<body>\n<div class=\"panel panel-default\">\n\t<div class=\"panel-heading\">\xe7\xa7\x92\xe6\x9d\x80\xe5\x95\x86\xe5\x93\x81\xe5\x88\x97\xe8\xa1\xa8</div>\n\t<table id=\"goodslist\" class=\"table\">\n\t\t<tr>\n\t\t\t<td>\xe5\x95\x86\xe5\x93\x81\xe5\x90\x8d\xe7\xa7\xb0</td><td>\xe5\x95\x86\xe5\x93\x81\xe5\x9b\xbe\xe7\x89\x87</td><td>\xe5\x95\x86\xe5\x93\x81\xe5\x8e\x9f\xe4\xbb\xb7</td><td>\xe7\xa7\x92\xe6\x9d\x80\xe4\xbb\xb7</td><td>\xe5\xba\x93\xe5\xad\x98\xe6\x95\xb0\xe9\x87\x8f</td><td>\xe8\xaf\xa6\xe6\x83\x85</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>iphoneX</td>\n\t\t\t<td><img width=\"100\" height=\"100\" src=\"/img/iphoneX.png\" /></td>\n\t\t\t<td>8765.0</td>\n\t\t\t<td>0.01</td>\n\t\t\t<td>3</td>\n\t\t\t<td><a href=\"/goods/to_detail/1\">\xe8\xaf\xa6\xe6\x83\x85</a></td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>\xe5\x8d\x8e\xe4\xb8\xbaMate9</td>\n\t\t\t<td><img width=\"100\" height=\"100\" src=\"/img/meta9.png\" /></td>\n\t\t\t<td>3212.0</td>\n\t\t\t<td>0.01</td>\n\t\t\t<td>9</td>\n\t\t\t<td><a href=\"/goods/to_detail/2\">\xe8\xaf\xa6\xe6\x83\x85</a></td>\n\t\t</tr>\n\t</table>\n</div>\n</body>\n</html>"
127.0.0.1:6379>
二、url缓存
比如针对不同的商品,对应着不同的商品详情,不同的商品详情是根据url中的特定字符来区分的,比如商品的id,在缓存的时候要缓存进去。
package com.mydre.miaosha.redis;
public class GoodsKey extends BasePrefix{
private GoodsKey(int expireSeconds, String prefix){
super(expireSeconds, prefix);
}
public static GoodsKey getGoodsList = new GoodsKey(60, "gl");//设置页面缓存的时间是1分钟,在缓存的同时保持页面的更新
public static GoodsKey getGoodsDetailt = new GoodsKey(60, "gd");//
}
@RequestMapping(value="/to_detail/{goodsId}", produces="text/html")//路由是这样写的 {goodsId}
@ResponseBody
public String detail(Model model, MiaoshaUser user, @PathVariable("goodsId")long goodsId,HttpServletRequest request,HttpServletResponse response) {
// if(user == null){
// return "login";
// }
//从redis数据库中获取数据
//取缓存
String html = redisService.get(GoodsKey.getGoodsDetailt, "" + goodsId, String.class);
if(!StringUtils.isEmpty(html)){
return html;
}
//数据的存取是根据内部的逻辑来的,但是页面的缓存是根据key来的,一个goodsId可以标记一个缓存。
GoodsVo oneGoodsVo = goodsService.getOneGoodsVoById(goodsId);
long startAt = oneGoodsVo.getStartDate().getTime();
long endAt = oneGoodsVo.getEndDate().getTime();
long current = System.currentTimeMillis();
int remainSeconds = 0;
int miaoshaStatus = 0;//0未开始 1正在进行 2已结束
if(current < startAt){//秒杀还未开始
miaoshaStatus = 0;
remainSeconds = (int)((startAt - current)/1000);
}else if(current > endAt){//秒杀已经结束
miaoshaStatus = 2;
remainSeconds = -1;
}else{//秒杀正在进行
miaoshaStatus = 1;
remainSeconds = 0;
}
model.addAttribute("goods", oneGoodsVo);
model.addAttribute("user", user);
model.addAttribute("remainSeconds", remainSeconds);
model.addAttribute("miaoshaStatus", miaoshaStatus);
//return "goods_detail";
//手动渲染
SpringWebContext springWebContext = new SpringWebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);
html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", springWebContext);
redisService.set(GoodsKey.getGoodsDetailt, "" + goodsId, html);
if(!StringUtils.isEmpty(html)){
return html;
}
return html;
}
查看redis缓存:
127.0.0.1:6379> keys GoodsKey:gd1
1) "GoodsKey:gd1"
127.0.0.1:6379> keys GoodsKey:gd2
1) "GoodsKey:gd2"
127.0.0.1:6379> get GoodsKey:gd1
说明已经缓存成功。