title: Understanding Memcache
body: <p>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:</p>
  
  <ol>
  <li>Is memcache programming only about app engine? Is there nothing to do with android side? (I mean if any coding needed at android application?)</li>
  <li>I used JPA to program my app engine app. Can I use low level API for memcache?</li>
  <li><p>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?.</p>
  
  <pre><code>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);
  }
  }
  }
  </code></pre></li>
  <li><p>Do you know any source that has a working sample app or something?</p></li>
  </ol>
  
  <p>Maybe you laugh reading these questions but I really cannot figure these things out. Thanks in advance.</p>
  
  <p><strong>Edit:</strong> I tried to add memcache to my code unsuccessfully, can you take a look at the code please?</p>
  
  <pre><code>@SuppressWarnings({ "unchecked", "unused" })
  @ApiMethod(name = "queryDesire")
  public CollectionResponse&lt;Desire&gt; 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&lt;Desire&gt; execute = null;        
      try {
          String keyDesire = "mem_" + name;
          List&lt;Desire&gt; memDesire = (List&lt;Desire&gt;) memcache.get(keyDesire);
          if (memDesire == null) {
              mgr = getEntityManager();
              Query query2 = mgr.createQuery("select i from Desire i where i.ctgry = :name ");
              if (cursorString != null &amp;&amp; cursorString != "") {
                  cursor = Cursor.fromWebSafeString(cursorString);
                  query2.setHint(JPACursorHelper.CURSOR_HINT, cursor);
              }
              if (limit != null) {
                  query2.setFirstResult(first);
                  query2.setMaxResults(limit);
              }
              execute = (List&lt;Desire&gt;) query2.setParameter("name", name).getResultList();
              cursor = JPACursorHelper.getCursor(execute);
              if (cursor != null)
                  cursorString = cursor.toWebSafeString();
              for (Desire obj : execute)
                  ;               
              CollectionResponse.&lt;Desire&gt; builder().setItems(execute)
              .setNextPageToken(cursorString).build();
              memcache.put("mem_cache", queryDesire);
          }                   
          return CollectionResponse.&lt;Desire&gt; builder().setItems(execute)
                  .setNextPageToken(cursorString).build();
          }
      finally {
          mgr.close();
      }
  
  
  }
  </code></pre>