--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/04/23 15:25:52 664 +++ android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/05/04 03:48:37 705 @@ -30,6 +30,8 @@ String login; String password; + + MemcachedClient memcache = null; @Override @@ -38,18 +40,23 @@ login = getServletContext().getInitParameter("login"); password = getServletContext().getInitParameter("password"); + + try { + memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + } catch (IOException e) { + throw new ServletException(e); + } } - protected String doLookup(String appId) throws IOException { - MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); - + protected String doLookup(String query) throws IOException { + - String key = "marketstats:" + appId; - String response = (String) c.get(key); + String key = "marketstats:" + query; + String response = (String) memcache.get(key); if (response == null) { - response = doLookupWorker(appId); - c.set(key, TIMEOUT, response); + response = doLookupWorker(query); + memcache.set(key, TIMEOUT, response); response += ""; } else { response += ""; @@ -82,14 +89,14 @@ session.flush(); start +=10; - if (start >= 30) + if (start >= 20) break; //emergency brake } while ( start < commentsCb.getEntryCount() ); } - protected String doLookupWorker(String appId) { + protected String doLookupWorker(String query) { final StringBuilder sb = new StringBuilder(); @@ -98,7 +105,7 @@ AppsRequest appsRequest = AppsRequest.newBuilder() - .setAppId(appId) + .setQuery(query) .setStartIndex(0).setEntriesCount(10) .setWithExtendedInfo(true) .build(); @@ -113,33 +120,39 @@ session.append(appsRequest, appsCb); session.flush(); + + String appId = appsCb.getAppId(); + + if (appId != null) { - loadComments(appId, session, "da", commentBeans); - loadComments(appId, session, "en", commentBeans); - - Collections.sort(commentBeans); - - sb.append("-----------------------------------------------------------------\n"); - for (CommentBean c : commentBeans) { - sb.append("User: " + c.author + " (" + c.locale + ")\n"); - sb.append("Rating: " + c.rating + "\n"); - sb.append("Time: " + new Date(c.time).toString() + "\n"); - sb.append( c.text + "\n"); - sb.append("\n"); - + loadComments(appId, session, "da", commentBeans); + loadComments(appId, session, "en", commentBeans); + + Collections.sort(commentBeans); + + sb.append("-----------------------------------------------------------------\n"); + for (CommentBean c : commentBeans) { + sb.append("User: " + c.author + " (" + c.locale + ")\n"); + sb.append("Rating: " + c.rating + "\n"); + sb.append("Time: " + new Date(c.time).toString() + "\n"); + sb.append( c.text + "\n"); + sb.append("\n"); + + } + sb.append("Comments: " + commentBeans.size() + "\n"); } - sb.append("Comments: " + commentBeans.size() ); + sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes"); return sb.toString(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String appId = request.getParameter("appId"); + String query = request.getParameter("query"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); - out.print( "
" + doLookup(appId) + "
" ); + out.print( "
" + doLookup(query) + "
" ); } }