2012-02-07 15 views
5

我正在Google應用引擎上製作我的第一個應用。之前我只是檢查我的應用程序的正確結果。但是,我的應用程序開始響應真的很晚。然後我通過谷歌應用程序引擎文檔,現在開始使用appstats。我真的很陌生。我看了一段關於它的視頻,並得到了一些東西,但我仍然有點困惑。以下是我的應用程序的圖形爲一個登錄請求:什麼是模板擴展,如果我們的谷歌應用程序引擎應用程序,我們該如何減少它?

enter image description here

這裏下面是我LoginCheckServlet代碼:據谷歌應用程序引擎

public class LoginCheckServlet extends HttpServlet { 
    @SuppressWarnings("unchecked") 
    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
    throws IOException { 
     resp.setContentType("text/html"); 
     PrintWriter out = resp.getWriter(); 
     HttpSession session = req.getSession(true); 
     PersistenceManager pm = PMF.get().getPersistenceManager(); 
     try 
     { 
      List<User> result = null; 
      Query query = pm.newQuery(User.class); 
      query.setFilter("email == emailParam"); 
      query.declareParameters("String emailParam"); 
      result = (List<User>) query.execute(req.getParameter("email")); 
      if(result.size() == 0){ 
       out.println(0); 
      }else{ 
       String pwd = req.getParameter("password"); 
       String userPwd = result.get(0).getPassword(); 
       if(pwd.equals(userPwd)){ 
        result.get(0).setActive(true); 
        session.setAttribute("email", result.get(0).getEmail()); 
        session.setAttribute("name", result.get(0).getName()); 
        out.println("1"); 
       }else{ 
        out.println("2"); 
       } 
      } 
     }catch(Exception ex) 
     { 
      out.println(ex); 
     } 
     finally 
     { 
      pm.close(); 
     } 
    } 
} 

查詢所需的大部分時間它大約在50-100毫秒。但在圖中總共花費的時間是15167毫秒。而我的應用程序在演示文稿中所調用的模板擴展時間幾乎是140000毫秒。我不明白這是什麼模板擴展,爲什麼我的應用程序正在採取大量的?我如何減少它?可能是它的一個基本問題,但我對此很新,我搜索了但找不到幫助。提前致謝。

+0

這是您正在運行的實際代碼,並且結果是否一致?也許你已經定義了很多屬性?在問題中包含模型的定義。 – 2012-02-07 17:57:06

+0

您是否也對其他處理程序有相同的延遲問題,或者只有這個延遲問題? – proppy 2012-02-07 18:28:37

+0

其幾乎所有的處理程序 – Piscean 2012-02-07 21:11:14

回答

1

由於@allyourcode menitoend模板用於生成HTML。在Google應用引擎中構建的一些模板引擎是Django,jinja。

首先,我想告訴你,存儲密碼inclear不是一個好主意。確保它們被散列。如果你的網站被商業化並且被黑客入侵,oyur的客戶將會生氣。考慮使用散列庫。

其次爲了減少查詢時間,通過這個概念稱爲memcache.This將大大減少您的查詢時間。

下面是簡單的例子使用內存緩存: - 從google.appengine.ext進口分貝 從google.appengine.api進口的memcache

def top_arts(update = False): 
    key = 'top' 

    #Getting arts from memcache 
    arts = memcache.get(key) 

    #Check if key is defined in memcache 
    #or an update has been invoked 
    if update or not arts: 
    #Querying the Google Data store using GQL 
    arts = db.GqlQuery('SELECT * from Art ORDER BY created DESC LIMIT 10') 
    memcache.set(key, arts) 
    return arts 

您可以使用相同的功能,從內存緩存,然後再讀將數據寫入內存緩存

例如:用於從內存緩存讀取: - 藝術= top_arts()

寫入數據庫時​​

: -

#write your entry in database 
<some database code> 
#update memcache with this new entry 
top_arts(update=True) 
相關問題