开发者

Save objects in MemCache instead of Datastore

I have built an app using google app engine. The app works fine, but am currently thinking of optimizing it, and in particular I've heard of this concept of persisting objects to memcache instead of the datastore, as a means of quickly acessing frequently needed data.

Assume I have objects of this model:

class Ea(db.Model):  
    name = db.StringProperty()  
    code = db.StringProperty()

And I have say the following instance I want开发者_开发知识库 to keep in memcache

myea = Ea.new(name='John',code='@jo')

How do I do it?


If you care about the data, you should first write it to the datastore. You can invalidate the cache pre-write, then, depending on your application, recache post-write or on the first read.

Check out Nick's post on efficiently memcaching models; he presents a nice pattern, and will help you avoid some common issues.


The link specified by Robert Kluin works for db based model. For ndb based models you can use this.

def ndb_entity_to_protobuf(e):
    return ndb.ModelAdapter().entity_to_pb(e).Encode()

def protobuf_to_ndb_entity(pb):
    # precondition: model class must be imported
    return ndb.ModelAdapter().pb_to_entity(entity_pb.EntityProto(pb))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜