NDB query fetch() and ContextOptions

2019-02-26 19:01发布

问题:

I would like to disable context cache in only one of my queries. I thought I could do it like this:

MyModel.query(ancestor=user.key).fetch(100, options=ContextOptions(use_cache=False, use_memcache=False))

or

MyModel.query(ancestor=user.key).fetch(100, config=ContextOptions(use_cache=False, use_memcache=False))

But it does not seem to work for me. So my question is how do I disable cache and memcache for queries using fetch?

PS:

For get() it works perfectly:

MyModel.query(ancestor=user.key).get(use_cache=False, use_memcache=False)

Thanks!

回答1:

Caching is supported only for get()'s. 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).

If you are experiencing problems with some entities, which seem to be cached, you probably have to change context cache policy:

ctx.set_cache_policy(lambda key: False)

Argument of set_cache_policy has to be function taking one param (key) and returning boolean if the key has to be cached. Here it always returns False, so no entity will be cached.



回答2:

You should just be able to write

MyModel.query(........).fetch(limit, use_cache=False)

If that doesn't have the desired effect you're looking at some different bug. There should be no need to set a global cache policy for this purpose.

(It's true that at some point in the past, fetch() didn't support use_cache=...; but that's long been fixed. Also, there's no need to bother with use_memcache=...; it's not used by the query process at all.)