mirror of
https://github.com/qiurunze123/miaosha.git
synced 2023-11-19 22:41:03 +08:00
提交 页面级缓存 basecntroller
This commit is contained in:
parent
984f922b81
commit
5e0213d3a7
|
@ -0,0 +1,65 @@
|
||||||
|
package com.geekq.miaosha.controller;
|
||||||
|
|
||||||
|
import com.geekq.miaosha.redis.KeyPrefix;
|
||||||
|
import com.geekq.miaosha.redis.RedisService;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.thymeleaf.context.WebContext;
|
||||||
|
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class BaseController {
|
||||||
|
|
||||||
|
|
||||||
|
//加一个配置项
|
||||||
|
@Value("#{'${pageCache.enbale}'}")
|
||||||
|
private boolean pageCacheEnable;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ThymeleafViewResolver thymeleafViewResolver;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RedisService redisService;
|
||||||
|
|
||||||
|
|
||||||
|
public String render(HttpServletRequest request, HttpServletResponse response, Model model, String tplName, KeyPrefix prefix, String key) {
|
||||||
|
if(!pageCacheEnable) {
|
||||||
|
return tplName;
|
||||||
|
}
|
||||||
|
//取缓存
|
||||||
|
String html = redisService.get(prefix, key, String.class);
|
||||||
|
if(!StringUtils.isEmpty(html)) {
|
||||||
|
out(response, html);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//手动渲染
|
||||||
|
WebContext ctx = new WebContext(request,response,
|
||||||
|
request.getServletContext(),request.getLocale(), model.asMap());
|
||||||
|
html = thymeleafViewResolver.getTemplateEngine().process(tplName, ctx);
|
||||||
|
if(!StringUtils.isEmpty(html)) {
|
||||||
|
redisService.set(prefix, key, html);
|
||||||
|
}
|
||||||
|
out(response, html);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void out(HttpServletResponse res, String html){
|
||||||
|
res.setContentType("text/html");
|
||||||
|
res.setCharacterEncoding("UTF-8");
|
||||||
|
try{
|
||||||
|
OutputStream out = res.getOutputStream();
|
||||||
|
out.write(html.getBytes("UTF-8"));
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,7 +27,7 @@ import java.util.List;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/goods")
|
@RequestMapping("/goods")
|
||||||
public class GoodsController {
|
public class GoodsController extends BaseController {
|
||||||
private static Logger log = LoggerFactory.getLogger(GoodsController.class);
|
private static Logger log = LoggerFactory.getLogger(GoodsController.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -54,21 +54,9 @@ public class GoodsController {
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String list(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser user) {
|
public String list(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser user) {
|
||||||
model.addAttribute("user", user);
|
model.addAttribute("user", user);
|
||||||
//取缓存
|
|
||||||
String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
|
|
||||||
if(!StringUtils.isEmpty(html)) {
|
|
||||||
return html;
|
|
||||||
}
|
|
||||||
List<GoodsVo> goodsList = goodsService.listGoodsVo();
|
List<GoodsVo> goodsList = goodsService.listGoodsVo();
|
||||||
model.addAttribute("goodsList", goodsList);
|
model.addAttribute("goodsList", goodsList);
|
||||||
SpringWebContext ctx = new SpringWebContext(request,response,
|
return render(request,response,model,"goods_list",GoodsKey.getGoodsList,"");
|
||||||
request.getServletContext(),request.getLocale(), model.asMap(), applicationContext );
|
|
||||||
//手动渲染
|
|
||||||
html = viewResolver.getTemplateEngine().process("goods_list", ctx);
|
|
||||||
if(!StringUtils.isEmpty(html)) {
|
|
||||||
redisService.set(GoodsKey.getGoodsList, "", html);
|
|
||||||
}
|
|
||||||
return html;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value="/to_detail2/{goodsId}",produces="text/html")
|
@RequestMapping(value="/to_detail2/{goodsId}",produces="text/html")
|
||||||
|
|
|
@ -6,6 +6,8 @@ spring.thymeleaf.content-type=text/html
|
||||||
spring.thymeleaf.enabled=true
|
spring.thymeleaf.enabled=true
|
||||||
spring.thymeleaf.encoding=UTF-8
|
spring.thymeleaf.encoding=UTF-8
|
||||||
spring.thymeleaf.mode=HTML5
|
spring.thymeleaf.mode=HTML5
|
||||||
|
#是否开启缓存
|
||||||
|
pageCache.enbale=true
|
||||||
|
|
||||||
#mybatis
|
#mybatis
|
||||||
mybatis.type-aliases-package=com.geekq.miaosha.domain
|
mybatis.type-aliases-package=com.geekq.miaosha.domain
|
||||||
|
|
Loading…
Reference in New Issue
Block a user