首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >no_cache()在Mongoengine中对querySet的影响

no_cache()在Mongoengine中对querySet的影响
EN

Stack Overflow用户
提问于 2018-12-03 14:27:22
回答 1查看 743关注 0票数 4

在mongoengine的官方文档中,它说从0.8开始,no_cache()被添加到mongoengine中。它能给我们带来什么好处?no_cache申请的典型场景是什么?

EN

回答 1

Stack Overflow用户

发布于 2019-06-05 03:41:53

Mongoengine maintainer here -默认情况下(以及历史上),当你在查询集上迭代时,mongoengine会缓存所有结果。这样做的好处是,如果您重新迭代相同的变量,则不会触发查询,但缺点是将所有内容都保存在内存中。即:

代码语言:javascript
复制
class User(Document):
    pass

users = User.objects()         # users is a queryset, it didn't hit the db yet

_ = [us for us in in users]    # hits the db and caches all user instances in the users object
_ = [us for us in in users]    # does not hit the db anymore, uses the users cached data


users = User.objects().no_cache()
_ = [us for us in in users]    # hits the db and caches all user instances
_ = [us for us in in users]    # hits the db again

使用缓存听起来像是一个好主意,但在实践中,您很少对同一查询集迭代两次,如果您正在迭代非常大的集合,那么内存消耗可能会成为一个问题。

请注意,将来可能会更改为默认使用no_cache版本

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53588542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档