forked from qiurunze123/miaosha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qiurunze
committed
Dec 10, 2018
1 parent
984f922
commit 5e0213d
Showing
3 changed files
with
69 additions
and
14 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/com/geekq/miaosha/controller/BaseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters