簡體   English   中英

了解內存緩存

[英]Understanding Memcache

我是新手android開發人員。 我正在嘗試構建基於應用程序引擎的應用程序。 我現在可以使其工作,但我意識到我需要使用內存緩存來優化數據庫訪問。 但是我很難理解一些基本概念。 所以我的問題如下:

  1. Memcache編程僅關於App Engine嗎? 與android無關嗎? (我的意思是在Android應用程序中是否需要任何編碼?)
  2. 我使用JPA對我的App Engine應用進行編程。 我可以將低級API用於內存緩存嗎?
  3. 我在一本書上得到了這個示例,但是它使用了許多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); } } } 
  4. 您是否知道任何來源的示例應用程序正常工作?

也許您在閱讀這些問題時笑了,但我真的無法弄清楚這些事情。 提前致謝。

編輯:我嘗試將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用於服務器端,即App Engine。 它通常用於加快從服務器到客戶端的響應,但與客戶端代碼無關。 換句話說,如果您在App Engine端使用Memcache,則無需在客戶端進行任何更改。

  2. 是的,您可以將低級API用於Memcache。

  3. 請參閱對問題1的答復。無論應用程序如何與服務器通信,都可以使用Memcache。

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM