Sign in

Google Cookbook - Google App Engine

Cached one-to-many collections
Posted by skibaa on Sat 03 Jan 2009 in Datastore
User ratings:
Extend ReferenceProperty so the collection would not be fetched every time you iterate though it. So instead of returning a query the back reference modelname_set property will return the whole collection from memory.
class Master(db.Model):
pass
class Detail(db.Model):
master=CachedReferenceProperty(Master)
m=Master()
m.save()
d=Detail(master=m)
d.save()
m.detail_set #fetches and returns [d]
m.detail_set #returns cached value, no DataStore access
delete m.detail_set #resets the cached value
m.detail_set #value fetched again

Please notice that in the returned collection the hidden attribute __RESOLVED_master is already set to the same instance of Master, so you can go back and forth like this effectively:
m.detail_set[0].master.detail_set[-1].master

Similar code would fetch both Master and Detail again and again with original ReferenceProperty.
Attached files
cached_db.pyViewDownload

Comments (0)

Sign in to add comment.