Django 在他们的文档中声明所有查询集都会自动缓存,https://docs.djangoproject.com/en/dev/topics/db/queries/#caching-and-querysets。但他们对这个功能的细节并不是特别具体。
他们给出的示例是将 qs 保存在 python 变量中,第一个之后的后续调用将从缓存中获取。
queryset = Entry.objects.all()
print([p.headline for p in queryset]) # 评估查询集。
print([p.pub_date for p in queryset]) # 重用评估中的缓存。
因此,即使在用户加载视图时随后在没有变量的情况下进行了两次精确的查询集调用,结果是否不会被缓存?
# When the user loads the homepage, call number one (not cached)
def home(request):
entries = Entry.objects.filter(something)
return render_to_response(...)
# Call number two, is this cached automatically? Or do I need to import cache and
# manually do it? This is the same method as above, called twice
def home(request):
entries = Entry.objects.filter(something)
return render_to_response(...)
抱歉,如果这令人困惑,我将方法粘贴了两次,以使用户看起来像是两次调用它,它只是一种方法。条目是否自动缓存?
谢谢