Android 了解Memcache
我是一个android开发新手。我正在尝试构建一个基于应用程序引擎的应用程序。我现在可以让它工作,但我意识到我需要使用memcache来优化数据库访问。但我不太理解一些基本概念。因此,我的问题如下:Android 了解Memcache,android,google-app-engine,memcached,Android,Google App Engine,Memcached,我是一个android开发新手。我正在尝试构建一个基于应用程序引擎的应用程序。我现在可以让它工作,但我意识到我需要使用memcache来优化数据库访问。但我不太理解一些基本概念。因此,我的问题如下: memcache编程是否只针对应用程序引擎?这与android端无关吗?(我是说android应用程序是否需要任何编码?) 我使用JPA来编程我的应用程序引擎应用程序。我可以为memcache使用低级API吗 我在一本书中得到了这个例子,但它使用了许多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);
}
}
}
@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();
}
}
@SuppressWarnings({“未选中”、“未使用”})
@ApiMethod(name=“queryDesire”)
公共收集响应查询需求(
@可为null的@Named(“游标”)字符串cursorString,
@可为空@Named(“limit”)整数限制,
@可空@Named(“first”)整数优先,
@可空@Named(“name”)字符串名称){
EntityManager mgr=null;
游标=空;
List execute=null;
试一试{
字符串keyDesire=“mem_”+名称;
List memDesire=(List)memcache.get(keydire);
if(memDesire==null){
mgr=getEntityManager();
Query query2=mgr.createQuery(“从i中选择i,其中i.ctgry=:name”);
if(cursorString!=null&&cursorString!=“”){
cursor=cursor.fromWebSafeString(cursorString);
query2.setHint(JPACursorHelper.CURSOR\u HINT,CURSOR);
}
如果(限制!=null){
查询2.setFirstResult(第一个);
查询2.setMaxResults(限制);
}
execute=(List)query2.setParameter(“name”,name).getResultList();
cursor=JPACursorHelper.getCursor(执行);
如果(光标!=null)
cursorString=cursor.towerbsafesting();
for(愿望对象:执行)
;
CollectionResponse.builder().setItems(执行)
.setNextPageToken(cursorString.build();
memcache.put(“mem_cache”,queryDesire);
}
return CollectionResponse.builder().setItems(执行)
.setNextPageToken(cursorString.build();
}
最后{
经理关闭();
}
}
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;
}
private static final MemcacheService memcache=MemcacheServiceFactory.getMemcacheService();
公共静态数组列表getTimeZones(){
ArrayList时区=(ArrayList)memcache.get(“时区”);
如果(时区==null){
//此方法从属性文件中读取时区
时区=准备时区();
memcache.put(“时区”,时区);
}
返回时区;
}
安德烈,谢谢你全面的回答。我想再问一件事。我想在用户调用查询结果后将其缓存,以便其他用户可以从缓存中使用。要证明这一点,逻辑应该是什么?i、 e.memcache是一个单独的类,我在查询后调用memcache?您可以使用与我的示例相同的逻辑:首先检查memcache,如果所需的项不存在,则执行查询以检索它,将其放入memcache,返回响应。您需要确定哪个键标识您缓存的查询,并确保结果(a)可序列化,并且(b)小于1MB。嘿,Andrei,我试着按照您所说的做了上面的编辑。你能看一下吗。我无法设置返回语句。还有memcache.put(“mem_cache”,queryDesire);此处有一个错误,无法将查询愿望解析为变量。