这是我的代码:
public class The_Big_Bang_Theory_Quiz_HighscoreserverServlet extends httpServlet {public voID doGet(httpServletRequest req,httpServletResponse resp) throws IOException { String game = req.getParameter("game"); String name = req.getParameter("name"); String pointsstr = req.getParameter("points"); String behaviorStr = req.getParameter("behavior"); int behavior = 0; // 0 = upload,1 = download if (behaviorStr != null) { try { behavior = Integer.parseInt(behaviorStr); } catch (NumberFormatException e) { behavior = 0; } } if (behavior == 0) { int points = 0; if (pointsstr != null) { try { points = Integer.parseInt(pointsstr); } catch (NumberFormatException e) { points = 0; } } if (points > 0 && name != null) { addHighscore(game,name,points); } } else { String maxStr = req.getParameter("max"); int max = 1000; if (maxStr != null) { try { max = Integer.parseInt(maxStr); } catch (NumberFormatException e) { max = 1000; } } returnHighscores(resp,game,max); }}private voID returnHighscores(httpServletResponse resp,String game,int max) { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key gameKey = KeyFactory.createKey("game",game); query query = new query("highscore",gameKey); query.addSort("points",query.sortDirection.DESCENDING); List<Entity> highscores = datastore.prepare(query).asList(FetchOptions.Builder.withlimit(max)); for(Entity e : highscores) { try { resp.getWriter().println(e.getProperty("name") + ";" +e.getProperty("points")); } catch (IOException exc) { exc.printstacktrace(); } }}private voID addHighscore(String game,String name,int points) { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key gameKey = KeyFactory.createKey("game",game); Entity highscore = new Entity("highscore",gameKey); highscore.setProperty("name",name); highscore.setProperty("points",points); datastore.put(highscore);}}
我读了一些关于BlobStore的内容.这是一种更好的方法吗?
解决方法 我有同样的问题,我使用GEA的缓存机制来解决问题.基本上,Cache是分布式HasMap
一些代码:
创建地图:
try { cache = CacheManager.getInstance().getCacheFactory().createCache( new ConcurrentHashMap<String,category>()); } catch (CacheException e) { Logger .getLogger(TipsDAO.class.getname()) .severe( "unable to cretate cache using an internal ConcurrentHashMap"); cache = new ConcurrentHashMap<String,category>(); }
对于每个阅读d出,您首先检查地图,如果您发现我返回,如果您没有找到它,您从数据库中读取并在返回之前将其放入地图中.
if (cache.containsKey(cat)) { return (category) cache.get(cat); } try { query query = entityManager .createquery("SELECT FROM category WHERE name = ?1"); query.setParameter(1,cat); category temp = (category) query.getSingleResult(); cache.put(cat,temp); return temp; } catch (Exception e) { LOG.severe(e.getMessage()); return null; }
对于DB的每个写 *** 作,您也可以写入Map
cache.put(cat.getname(),cat);总结
以上是内存溢出为你收集整理的android – Google App Engine – 太多数据存储读取 *** 作全部内容,希望文章能够帮你解决android – Google App Engine – 太多数据存储读取 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)