Ale*_*exW 8 python django django-templates django-cache django-caching
我注意到随机一些页面需要 2 到 12 秒才能加载,我安装了调试工具栏,我知道我的查询都很有效(即没有重复),工具栏显示它们都以毫秒为单位运行。
我决定关注的一个特定页面是我的搜索页面,它使用 haystack 和弹性搜索。
我有一个查询 haystack 的函数,我有一个计时器,它在服务器端函数的开头和结尾运行,然后生成查询时间,从 0.01 到 0.2 秒不等,无论哪种方式都非常快(下面的视图示例) )。但是页面可能需要很长时间才能随机加载。
我向 DJDT 添加了模板计时面板,但它不支持 Django 2.x,但它仍然显示计时结果,从 2000 毫秒到 10000 毫秒不等
这让我研究了我在这篇文章中遇到的模板渲染(django:加快模板渲染性能的指南)。虽然我对上面提到的很多事情都不熟悉,但我确实研究了缓存。我已将以下内容添加到我的 settings.py 文件中:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-gugu-cache',
}
}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR + '/templates/',
],
'APP_DIRS': False,
'OPTIONS': {
'debug' : DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.request',
'django.template.context_processors.i18n',
'itapp.context_processors.site_links',
'itapp.context_processors.quick_jump_links',
],
'loaders': [
('django.template.loaders.cached.Loader', [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]),
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
在我的基本模板中,我有一个菜单,它根据权限显示或隐藏项目,还根据模型中的站点类型呈现菜单,所以我认为这将是一个缓存的好东西,因为一旦菜单已经决定对于用户和数据库已被查询它不会改变。(至少我认为这是我应该做的?)
所以我将以下内容添加到我的基本模板中:
{% load static %} {% load extras %} {% load i18n %} {% load cache %}
{% cache 500 sidebar %}
{% if user.is_active and user.is_staff %}
<a class="dropdown-item preview-item" href="{% url 'admin:index' %}">
<p class="preview-subject mb-1">{% trans 'Admin' %}</p>
...
{% if user.is_authenticated %}
...etc all the html and template logic for the side bar
{% end cache %}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,我在这里有正确的方法吗?我怎么知道侧边栏的缓存是否真的有效?除了等待查看页面加载速度是否缓慢之外,我如何证明这一点?
谢谢
视图.py
@login_required
def search(request):
from haystack.query import SearchQuerySet
from haystack.inputs import AutoQuery, Exact, Clean
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
start = datetime.now()
query = request.GET['q']
sqs = SearchQuerySet().filter(content=AutoQuery(query))
results = sqs
result_count = results.count()
end = datetime.now()
search_duration = (end - start).total_seconds()
return render(request, 'search/search_no_pag.html', {
'result_count' : result_count,
'query' : query,
'results': results,
'search_duration' : search_duration
})
Run Code Online (Sandbox Code Playgroud)
从实际监控生产中的应用程序中获得的最佳结果。它可以是从带有时间的 HTTP 日志开始的任何东西,也可以是专用于 Django 的成熟的 APM,可以总结每个视图的模板渲染。
这就是你知道的:
请查看那里的示例:https : //scoutapm.com/blog/monitoring-a-django-app-with-scout
如果与类似的工作条件相比,部署后“渲染”部分变得更小,那么您就可以证明某些事情有所帮助。
从 Django 1.11 开始,如果您不使用DEBUG
mode运行,则默认启用缓存加载器。
如果未指定 OPTIONS['loaders'] 且 OPTIONS['debug'] 为 False(后一个选项默认为 DEBUG 的值),则会自动启用此加载器。
https://docs.djangoproject.com/en/2.2/ref/templates/api/#django.template.loaders.cached.Loader
这让我想到了最后一点:您的问题可能是由DEBUG
模式引起的。这让我想到了我的第一句话:衡量生产绩效。