NDB 查询 fetch() 和 ContextOptions [英] NDB query fetch() and ContextOptions

查看:23
本文介绍了NDB 查询 fetch() 和 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))

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

但它似乎对我不起作用.所以我的问题是如何使用 fetch 为查询禁用缓存和内存缓存?

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

附注:

对于 get() 它完美地工作:

For get() it works perfectly:

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

谢谢!

推荐答案

缓存仅支持 get() 的.来自文档:

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

查询不会在任何缓存中查找值.但是,查询结果是如果缓存策略如此写回上下文缓存(但永远不要去Memcache).

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)

set_cache_policy 的参数必须是一个函数,如果必须缓存键,则返回一个参数(键)并返回布尔值.这里它总是返回 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.

这篇关于NDB 查询 fetch() 和 ContextOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆