我是 GAE 的 memcache 的新手,我需要这方面的帮助。 基本上,由于我没有使用内存缓存,我有一个超过数据存储读取操作限制的数据存储。我的数据存储有最少的写入,但有很多读取,每次有写入时,它应该可用于读取。由于该网站已经启动,我需要快速解决它,所以我需要设计方面的帮助。所以事情是,只要在数据存储中有写入,新条目就应该被 memcached。我还想知道如何将数据存储复制到内存缓存。与此同时,我正在处理它,但由于该网站已启动,我在这里询问它,手头没有任何代码。
谢谢
更新:
Java 代码如下所示:
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
if(memcache.contains("LocationInfo"))
{
JSONArray js = new JSONArray((String)memcache.get("LocationInfo"));
result = new ArrayList<LocationInfo>();
for(int i = 0; i < js.length(); i++)
{
JSONObject jso = (JSONObject)js.get(i);
LocationInfo loc = new LocationInfo(jso);
result.add(loc);
}
}
else
{
q1= pm.newQuery(LocationInfo.class);
q1.setFilter(filter);
result = (List<LocationInfo>)q1.execute();
JSONArray js = new JSONArray();
for(LocationInfo loc : result)
{
js.put(loc.toJSON());
}
memcache.put("LocationInfo", js.toString());
}
最佳答案
from google.appengine.ext import db
from google.appengine.api import memcache
def top_arts(update = False):
key = 'top'
#Getting arts from memcache
arts = memcache.get(key)
#Check if key is defined in memcache
#or an update has been invoked
if update or not arts:
#Querying the Google Data store using GQL
arts = db.GqlQuery('SELECT * from Art ORDER BY created DESC LIMIT 10')
memcache.set(key, arts)
return arts
您可以使用相同的函数从内存缓存中读取数据,然后将数据写入 内存缓存
例如:
从内存缓存中读取:-
arts = top_arts()
写入数据库时:-
#write your entry in database
<some database code>
#update memcache with this new entry
top_arts(update=True)
关于java - Google App 引擎 memcached 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11201004/