GAE提供了简单实用的API和开发工具,结合已有的开发框架,Java开发人员可以很容易开发出自己的业务应用系统。本文介绍如何对页面进行优化。
GAE即Google App Engine,还不了解什么是Google App Engine的能够阅览一下它的介绍。
GAE供给了简略有用的API和开发工具,结合已有的开发结构,Java开发人员能够很简略开发出自己的事务运用体系。本次先介绍页面部分的功能优化技巧,只需要进行简略的设置和少数的编码,即可获得不错的功能进步。文中说到的技巧已经在本博客获得验证,从后来的统计数据中能够看到,主页的处理时刻从均匀400ms削减到了均匀26ms,功能进步了15倍!
App Engine功能优化:指定GAE的静态文件装备
在一般的httpd + tomcat的架构中,客户端对图片、css文件以及js文件等静态资源文件,会依据文件的lsat modified特点,尽量只会恳求一次,只有当文件进行了更新之后,才会从头恳求新的文件。
但是在GAE里边,假如你不进行静态文件的设置,默许情况下,是无法享用上面所提的优点的。下面来看看设置的文件/WEB-INF/Appengine-web.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <appengine-web-appxmlns="http://appengine.google.com/ns/1.0">
- <application>yourappid</application>
- <version>1</version>
- <static-files>
- <includepath="/**.jpg"/>
- <includepath="/**.png"/>
- <includepath="/**.gif"/>
- <includepath="/**.ico"/>
- <includepath="/**.css"/>
- <includepath="/**.js"/>
- </static-files>
- </appengine-web-app>
进行了上面的设置之后,你的运用能够得到较为显着的功能进步。
App Engine功能优化:运用Memcache服务进行页面缓存
GAE供给了Memcache服务,能够将常常运用到数据暂时存储在Memcache中,能够大大削减恳求的处理时刻,进步页面响应速度。下面供给几个代码比如,运用Servlet Filter技能,能够对常常拜访的页面进行缓存操作。
CacheSingleton.java
- packagehover.blog.servlet;
- importjavax.cache.Cache;
- importjavax.cache.CacheException;
- importjavax.cache.CacheFactory;
- importjavax.cache.CacheManager;
- importjavax.servlet.ServletException;
- importjava.util.Map;
- /**
- *@authorHover
- *@version1.0
- */
- publicclassCacheSingleton{
- privatestaticfinalCacheSingletoninstance=newCacheSingleton();
- privateCachecache;
- privateCacheSingleton(){
- }
- publicstaticCacheSingletongetInstance(){
- returninstance;
- }
- publicvoidinit(Mapprops)throwsServletException{
- try{
- CacheFactoryfactory=CacheManager.getInstance().getCacheFactory();
- cache=factory.createCache(props);
- }catch(CacheExceptione){
- thrownewServletException("cacheerror:"+e.getMessage(),e);
- }
- }
- publicCachegetCache(){
- returncache;
- }
- publicvoidclear(){
- if(cache!=null){
- cache.clear();
- }
- }
- }
因需要在多处当地拜访Cache,因而这儿运用了Singleton形式,能够在不同的Action中拜访同一个Cache实例。
WebCacheFilter
WebCacheFilter.java
- packagehover.blog.servlet;
- importjavax.cache.Cache;
- importjavax.servlet.*;
- importjavax.servlet.http.Cookie;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importjava.io.BufferedOutputStream;
- importjava.io.ByteArrayOutputStream;
- importjava.io.IOException;
- importjava.util.Collections;
- importjava.util.List;
- /**
- *@authorHover
- *@version1.0
- */
- @SuppressWarnings("unchecked")
- publicclassWebCacheFilterimplementsFilter{
- publicstaticfinalStringPAGE_PREFIX="/page";
- publicvoidinit(FilterConfigconfig)throwsServletException{
- CacheSingleton.getInstance().init(Collections.emptyMap());
- }
- publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,
- FilterChainchain)throwsIOException,ServletException{
- HttpServletRequestrequest=(HttpServletRequest)servletRequest;
- HttpServletResponseresponse=(HttpServletResponse)servletResponse;
- Cachecache=CacheSingleton.getInstance().getCache();
- if("post".equalsIgnoreCase(request.getMethod())||cache==null){
- chain.doFilter(servletRequest,servletResponse);
- }else{
- StringrequestPath=request.getRequestURI();
- StringqueryString=request.getQueryString();
- if(queryString!=null&&queryString.length()>0){
- requestPath+="?"+queryString;
- }
- StringcachePath=PAGE_PREFIX+requestPath;
- PageInfopage=null;
- try{
- page=(PageInfo)cache.get(cachePath);
- }
- catch(Exceptione){
- //typemis-match
- }
- if(page==null){//oncachecontent
- ByteArrayOutputStreambyteArrayOutputStream=newByteArrayOutputStream();
- GenericResponseWrapperwrapper=newGenericResponseWrapper(response,byteArrayOutputStream);
- chain.doFilter(request,wrapper);
- wrapper.flush();
- page=newPageInfo(wrapper.getStatus(),wrapper.getContentType(),wrapper.getHeaders(),
- wrapper.getCookies(),byteArrayOutputStream.toByteArray());
- if(page.getStatus()==HttpServletResponse.SC_OK){
- cache.put(cachePath,page);
- }
- }
- response.setStatus(page.getStatus());
- StringcontentType=page.getContentType();
- if(contentType!=null&&contentType.length()>0){
- response.setContentType(contentType);
- }
- for(Cookiecookie:(List)page.getCookies()){
- response.addCookie(cookie);
- }
- for(String[]header:(List)page.getResponseHeaders()){
- response.setHeader(header[0],header[1]);
- }
- response.setContentLength(page.getBody().length);
- BufferedOutputStreamout=newBufferedOutputStream(response.getOutputStream());
- out.write(page.getBody());
- out.flush();
- }
- }
- publicvoiddestroy(){
- }
- }
在初始化的时分,调用CacheSingleton.init()办法,初始化Memecache的调用接口。
WebCacheFilter只处理HTTP GET恳求,对后台数据的修正、删去、新增等操作,应该运用HTTP POST方法来提交数据。
下面将此Filter所用到的其他辅佐类列在下面:
FilterServletOutputStream.java
- packagehover.blog.servlet;
- importjavax.servlet.ServletOutputStream;
- importjava.io.IOException;
- importjava.io.OutputStream;
- /**
- *@authorHover