[英]Understanding Memcache
我是新手android开发人员。 我正在尝试构建基于应用程序引擎的应用程序。 我现在可以使其工作,但我意识到我需要使用内存缓存来优化数据库访问。 但是我很难理解一些基本概念。 所以我的问题如下:
我在一本书上得到了这个示例,但是它使用了许多HTTP参考。 此类示例是否也可用于android应用,还是仅用于网站使用?
public class ETagCacheServlet extends HttpServlet { private static final long serialVersionUID = 4308584640538822293L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MemcacheService cache = MemcacheServiceFactory .getMemcacheService(); String cacheKey = request.getRequestURI() + "." + "etag"; String result; if (!cache.contains(cacheKey) || !cache.get(cacheKey).equals(request .getHeader("If-None-Match"))) { String etag = Long.toString(System.currentTimeMillis()); response.setHeader("ETag", etag); cache.put(cacheKey, etag); result = "Loaded into cache at " + (new Date()); response.getWriter().write(result); } else { response.setStatus(304); } } }
您是否知道任何来源的示例应用程序正常工作?
也许您在阅读这些问题时笑了,但我真的无法弄清楚这些事情。 提前致谢。
编辑:我尝试将memcache添加到我的代码失败,请看一下代码吗?
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "queryDesire")
public CollectionResponse<Desire> queryDesire(
@Nullable @Named("cursor") String cursorString,
@Nullable @Named("limit") Integer limit,
@Nullable @Named("first") Integer first,
@Nullable @Named("name") String name){
EntityManager mgr = null;
Cursor cursor = null;
List<Desire> execute = null;
try {
String keyDesire = "mem_" + name;
List<Desire> memDesire = (List<Desire>) memcache.get(keyDesire);
if (memDesire == null) {
mgr = getEntityManager();
Query query2 = mgr.createQuery("select i from Desire i where i.ctgry = :name ");
if (cursorString != null && cursorString != "") {
cursor = Cursor.fromWebSafeString(cursorString);
query2.setHint(JPACursorHelper.CURSOR_HINT, cursor);
}
if (limit != null) {
query2.setFirstResult(first);
query2.setMaxResults(limit);
}
execute = (List<Desire>) query2.setParameter("name", name).getResultList();
cursor = JPACursorHelper.getCursor(execute);
if (cursor != null)
cursorString = cursor.toWebSafeString();
for (Desire obj : execute)
;
CollectionResponse.<Desire> builder().setItems(execute)
.setNextPageToken(cursorString).build();
memcache.put("mem_cache", queryDesire);
}
return CollectionResponse.<Desire> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
finally {
mgr.close();
}
}
Memcache用于服务器端,即App Engine。 它通常用于加快从服务器到客户端的响应,但与客户端代码无关。 换句话说,如果您在App Engine端使用Memcache,则无需在客户端进行任何更改。
是的,您可以将低级API用于Memcache。
请参阅对问题1的答复。无论应用程序如何与服务器通信,都可以使用Memcache。
Memcache的使用方式多种多样,因此您可能需要发布特定问题,我们可能会提供帮助。 同时,这是我代码中的一个示例。 我的应用经常需要时区,并且时区永远不会改变。 因此,使用Memcache加快响应速度是有意义的。
private static final MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(); public static ArrayList<String> getTimeZones() { ArrayList<String> timeZones = (ArrayList<String>) memcache.get("time_zones"); if (timeZones == null) { // This method reads time zones from a properties file timeZones = prepareTimeZones(); memcache.put("time_zones", timeZones); } return timeZones; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.