GAE/P: Migrating to NDB efficiently

81 views Asked by At

I'm finally upgrading from db to ndb (it is a much bigger headache than I anticipated...).

I used a lot of ReferenceProperty and I've converted these to KeyProperty. Now, every place where I used a ReferenceProperty I need to add an explicit get because it was previously done for me automatically.

My question relates to whether I should restructure my code to make it more efficient. I have many methods that use a KeyProperty and I need to do an explicit get(). I'm wondering if I should change these methods so that I am passing the entity to them instead of using the KeyProperty and a get().

Is the automatic memcaching for ndb good enough that I don't need to restructure? Or should I restructure my code to avoid repeated gets of the same entity?

We are not looking at huge inefficiencies here. But for a single HTTP GET/POST I might be getting the same entity 3-5 times.

1

There are 1 answers

0
Jaime Gómez On BEST ANSWER

In your case the In-Context Cache will take over and save you the db calls:

The in-context cache is fast; this cache lives in memory. When an NDB function writes to the Datastore, it also writes to the in-context cache. When an NDB function reads an entity, it checks the in-context cache first. If the entity is found there, no Datastore interaction takes place.

Each request will get a new context.