Dennis Dennis - 1 year ago 70
Python Question

Best practices for caching front page queries for Google App Engine (GAE)

Wanted to ask what is the best practices for caching front pages queries for google app engine whose datastore provide only eventual consistency (unless ancestor queries are used)

The issue I'm running into is if I put an item into the datastore, I can't immediately run a query to get the correct and most up-to-date results and based on the docs I'm reading, there's no way for me to know when all the index values (or just the relevant ones) have been updated to produce the updated query

I've set up a manual add into the cache in parallel but then I would never really have a point of entry to make sure the cache is in sync with the datastore, unless I set an arbitrary time for cache expiry...

Is there a way to get around this?


Answer Source

You can cache your query as usual, but in addition use _post_put_hook to invalidate current cached value. This way you will have almost up to date results in your cache.

class Foo(ndb.Model):
  CACHE_KEY = 'bla_cache'
  bla = ndb.StringProperty()

  def build_page(cls):
    result = memcache.get(CACHE_KEY)
    if result is None:
      result = cls.query().fetch(100)
    return result

  def _post_put_hook(self, future):