Google App Engine性能调优 - 页面性能优化
2009-09-06 00:00:00 来源:WEB开发网因需要在多处地方访问Cache,因此这里使用了Singleton模式,可以在不同的Action中访问同一个Cache实例。
WebCacheFilter
WebCacheFilter.java
package hover.blog.servlet;
import javax.cache.Cache;
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* @author Hover
* @version 1.0
*/
@SuppressWarnings("unchecked")
public class WebCacheFilter implements Filter {
public static final String PAGE_PREFIX = "/page";
public void init(FilterConfig config) throws ServletException {
CacheSingleton.getInstance().init(Collections.emptyMap());
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
Cache cache = CacheSingleton.getInstance().getCache();
if ("post".equalsIgnoreCase(request.getMethod()) || cache == null) {
chain.doFilter(servletRequest, servletResponse);
} else {
String requestPath = request.getRequestURI();
String queryString = request.getQueryString();
if (queryString != null && queryString.length() > 0) {
requestPath += "?" + queryString;
}
String cachePath = PAGE_PREFIX + requestPath;
PageInfo page = null;
try {
page = (PageInfo) cache.get(cachePath);
}
catch (Exception e) {
// type mis-match
}
if (page == null) { // on cache content
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GenericResponseWrapper wrapper = new GenericResponseWrapper(response, byteArrayOutputStream);
chain.doFilter(request, wrapper);
wrapper.flush();
page = new PageInfo(wrapper.getStatus(), wrapper.getContentType(), wrapper.getHeaders(),
wrapper.getCookies(), byteArrayOutputStream.toByteArray());
if (page.getStatus() == HttpServletResponse.SC_OK) {
cache.put(cachePath, page);
}
}
response.setStatus(page.getStatus());
String contentType = page.getContentType();
if (contentType != null && contentType.length() > 0) {
response.setContentType(contentType);
}
for (Cookie cookie : (List) page.getCookies()) {
response.addCookie(cookie);
}
for (String[] header : (List) page.getResponseHeaders()) {
response.setHeader(header[0], header[1]);
}
response.setContentLength(page.getBody().length);
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(page.getBody());
out.flush();
}
}
public void destroy() {
}
}
- ››Google运作经理Bryan Power给出的GOOGLE求职意见
- ››Google用户体验的十大设计原则
- ››Applying Styles and Themes - 应用Style和Theme ...
- ››Google Analytics(分析)能为网站带来什么
- ››apple cocoa内存管理笔记
- ››Apple关于iPad应用需要支持设备方向的要求
- ››Google goggles图片搜索 如何优化一个wap网站
- ››Google Docs将增加iPhone和Android编辑功能
- ››APP-V攻略之五:序列化Office2003
- ››App-V攻略之一:App-V Management Server部署
- ››App-V攻略之二:App-V Management Server配置
- ››App-V攻略之三:App-V Client部署
更多精彩
赞助商链接