NDB query fetch() and ContextOptions

Ralphz Source

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))


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?


For get() it works perfectly:

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




answered 6 years ago Stanislav Heller #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.

answered 6 years ago Guido van Rossum #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.)

comments powered by Disqus