[英]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.