Skip to content

Instantly share code, notes, and snippets.

@patkujawa-wf
Created December 30, 2014 19:14
Show Gist options
  • Save patkujawa-wf/456fd2343c198403d938 to your computer and use it in GitHub Desktop.
Save patkujawa-wf/456fd2343c198403d938 to your computer and use it in GitHub Desktop.
Automatic caching with google app engine ndb example
# Taken from http://ae-book.appspot.com/static/pgae-ndb-20121009.pdf
# Video at https://www.youtube.com/watch?v=xZsxWn58pS0#t=3383
## Automatic Caching
# • Two automatic caching features
# • “In-context” cache
# • Memcache storage
# • Same basic idea, difference in scope
# • Context cache starts empty for each request
# • Minimizes datastore interactions throughout the request handler code
context = ndb.get_context()
context.set_cache_policy(lambda key: True)
# • Memcache: global, distributed cache
# • Outlives requests
# • ndb handles serialization of model instance
def test_memcache_ok(key):
# ...
context = ndb.get_context()
context.set_memcache_policy(test_memcache_ok)
# • Can set caching policies on a per-class basis,
# overriding the global policy:
class Player(ndb.Model):
_use_cache = True
_use_memcache = False
# • Can even set a “datastore policy”!
class ProgressMeter(ndb.Model):
_use_cache = True
_use_memcache = True
_use_datastore = False
meter = ndb.IntegerProperty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment