简体   繁体   中英

Understanding Memcache

I am a newbie android developer. 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? Is there nothing to do with android side? (I mean if any coding needed at android application?)
  2. I used JPA to program my app engine app. Can I use low level API for memcache?
  3. I got this example on a book but it usses many HTTP references. Is this type of example usable for android app also or is it only for websites' usage?.

     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?

@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. 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.

  2. Yes, you can use low-level API for Memcache.

  3. See response to question 1. Memcache can be used regardless of how an app communicates with the server.

  4. Memcache is used in many different ways, so you may need to post a specific question, and we may be able to help. 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.

     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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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