NDB Caching When Using Projected Queries

2019-02-20 16:08发布

问题:

Could not find this specific question asked before yet. How does App Engine's NDB handle caching when using projected queries vs. full entity queries?

For example, if I do a projected query first..

MyModel.query().get(projected=['name'])

...and then do a regular query next...

MyModel.query().get()

...what will I get? The full entity? If so, was ANY part of first query automatically cached by NDB? Or is NDB able to make the distinction well, so the next time I run the projected query it is potentially pulled from cache?

回答1:

As far as I'm aware, query results are only cached in in-context cache, but results are not fetched from cache for queries.

key.get() will be cached and retrieved from cache, but query.get() won't be retrieved from cache.

https://developers.google.com/appengine/docs/python/ndb/cache

From the docs: Queries do not look up values in any cache. However, query results are written back to the in-context cache if the cache policy says so (but never to Memcache).

To answer the question regarding what you'll be getting, in both queries it appears that the results will be fetched from the datastore and not from cache.



回答2:

Projection query results are never cached. It would not be right if a get by key were to ever return a projection result. So no worries.