Ken Kinder
Ken Kinder

Reputation: 13140

App Engine: Is there a good general-purpose tool for caching datastore objects?

I'm looking at writing a super-class for all my Google App Engine datastore classes that looks something like this:

class MemcacheMixin(object):
    @classmethod
    def cache_get_by_id(cls, id, parent=None):
        if memcache.has_key(...):
            ...
    ...

class MyEntity(db.Model, MemcacheMixin):
    ...

obj = MyEntity.cache_get_by_id(...)

The idea being that I'll almost always want to cache datastore objects, and I don't want to repeat myself. I assume someone must have already addressed this need and written a general-purpose tool for caching this stuff that integrates nicely into how the datastore classes work.

But, I haven't found it. Any recommendations?

Upvotes: 3

Views: 403

Answers (2)

proppy
proppy

Reputation: 10504

NDB (the new datastore library developer by Guido and included in the App Engine 1.6.1 SDK ) has builtin (memcache and memory) caching facilites.

See the documentation for more details.

Upvotes: 9

philofinfinitejest
philofinfinitejest

Reputation: 4047

What you are probably looking for is a memoization decorator. Here's a good, simple example:

http://code.google.com/p/appengine-afterburner/source/browse/trunk/python/src/afterburner/experimental/memoize.py

Also related is the recommendation to cache the protocol buffer rather than the model instance, for performance:

http://blog.notdot.net/2009/9/Efficient-model-memcaching

Upvotes: 2

Related Questions