WEB开发网
开发学院软件开发Java Google App Engine性能调优 - 页面性能优化 阅读

Google App Engine性能调优 - 页面性能优化

 2009-09-06 00:00:00 来源:WEB开发网   
核心提示: 因需要在多处地方访问Cache,因此这里使用了Singleton模式,Google App Engine性能调优 - 页面性能优化(3),可以在不同的Action中访问同一个Cache实例,WebCacheFilter WebCacheFilter.javapackagehover.blog.s

因需要在多处地方访问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() { 
  } 
} 

上一页  1 2 3 4 5 6 7  下一页

Tags:Google App Engine

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接