Querying NDB () and ContextOptions

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 this does not work for me. So my question is how to disable cache and memcache for requests using fetch ?

PS:

For get (), it works fine:

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

Thanks!

+2
source share
2 answers

Caching is only supported for get (). From docs :

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

If you are having problems with some entities that seem to be cached, you may have to change the context cache policy:

 ctx.set_cache_policy(lambda key: False) 

The set_cache_policy argument must be a function that takes one parameter (key) and returns a boolean if the key should be cached. Here it always returns False, so no entity will be cached.

+2
source

You should just write

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

If this does not have the desired effect, you are looking at some other error. For this purpose, a global caching policy is not required.

(It is true that at some point in the past fetch () did not support use_cache = ..., but it was fixed for a long time. In addition, there is no need to worry about using use_memcache = ...; the query process in general.)

+4
source

Source: https://habr.com/ru/post/1443080/


All Articles