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搜索引擎的奥秘
- ››Google测试搜索结果页面右侧内容更丰富的信息栏
- ››Google Dart精粹:应用构建,快照和隔离体
- ››APP Loading页设计和App从当前页进入新页面交互
- ››App产品开发:App产品开发与推广的经验
- ››google的代码审查
- ››google analytics清晰追踪爬虫的爬行信息
- ››Google+中文用户在两千万Google+大军中是少数派
- ››Google AdWords最昂贵点击成本的20种关键词分类
- ››Google运作经理Bryan Power给出的GOOGLE求职意见
- ››Google用户体验的十大设计原则
- ››Applying Styles and Themes - 应用Style和Theme ...
更多精彩
赞助商链接