简体   繁体   English

了解内存缓存

[英]Understanding Memcache

I am a newbie android developer. 我是新手android开发人员。 I am trying to build a app engine based application. 我正在尝试构建基于应用程序引擎的应用程序。 I could make it work on for now but I realised I need to use memcache to optimize database accesses. 我现在可以使其工作,但我意识到我需要使用内存缓存来优化数据库访问。 But I have a bad time understanding some basic concepts. 但是我很难理解一些基本概念。 So my questions are as follows: 所以我的问题如下:

  1. Is memcache programming only about app engine? Memcache编程仅关于App Engine吗? Is there nothing to do with android side? 与android无关吗? (I mean if any coding needed at android application?) (我的意思是在Android应用程序中是否需要任何编码?)
  2. I used JPA to program my app engine app. 我使用JPA对我的App Engine应用进行编程。 Can I use low level API for memcache? 我可以将低级API用于内存缓存吗?
  3. I got this example on a book but it usses many HTTP references. 我在一本书上得到了这个示例,但是它使用了许多HTTP参考。 Is this type of example usable for android app also or is it only for websites' usage?. 此类示例是否也可用于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); } } } 
  4. Do you know any source that has a working sample app or something? 您是否知道任何来源的示例应用程序正常工作?

Maybe you laugh reading these questions but I really cannot figure these things out. 也许您在阅读这些问题时笑了,但我真的无法弄清楚这些事情。 Thanks in advance. 提前致谢。

Edit: I tried to add memcache to my code unsuccessfully, can you take a look at the code please? 编辑:我尝试将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();
    }


}
  1. Memcache is used on the server-side, ie App Engine. Memcache用于服务器端,即App Engine。 It is often used to speed up the responses from the server to the client, but it is not related to the client code. 它通常用于加快从服务器到客户端的响应,但与客户端代码无关。 In other words, no changes are necessary on the client side if you use Memcache on App Engine side. 换句话说,如果您在App Engine端使用Memcache,则无需在客户端进行任何更改。

  2. Yes, you can use low-level API for Memcache. 是的,您可以将低级API用于Memcache。

  3. See response to question 1. Memcache can be used regardless of how an app communicates with the server. 请参阅对问题1的答复。无论应用程序如何与服务器通信,都可以使用Memcache。

  4. Memcache is used in many different ways, so you may need to post a specific question, and we may be able to help. Memcache的使用方式多种多样,因此您可能需要发布特定问题,我们可能会提供帮助。 Meanwhile, here is an example from my code. 同时,这是我代码中的一个示例。 Time zones are frequently required for my app, and they never change. 我的应用经常需要时区,并且时区永远不会改变。 So it makes sense to use Memcache to speed up the response. 因此,使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM