piyush piyush - 11 months ago 83
Java Question

Google App engine memcached design

I am new to memcache of GAE and I need a help in this.
Basically, I have a datastore which exceeded the Datastore Read Operations limit because of the fact that I didn't use memcache. My datastore has minimal writes but many reads and every time there's a write, it should be available for the read. Since, the site is up and I need a quick resolution for it so I need a design help in this. So the thing is, whenever there's a write in the datastore the new entry should get memcached. One more thing I would like to know that how the datastore can be replicated to the memcache. In parallel, I am working on it but since the site is up I am asking it here without any code in hand.



Java code looks like this:

MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
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);
q1= pm.newQuery(LocationInfo.class);
result = (List<LocationInfo>)q1.execute();
JSONArray js = new JSONArray();
for(LocationInfo loc : result)
memcache.put("LocationInfo", js.toString());

Answer Source
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

You can use the same function for reading from memcache and then writing data into memcache


for reading from memcache:-

 arts = top_arts()

when writing into database:-

#write your entry in database
<some database code>
#update memcache with this new entry