在 Django 中缓存查询结果 [英] Caching query results in django

查看:27
本文介绍了在 Django 中缓存查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找一种方法来缓存不会随频率变化的查询结果.例如,电子商务中的产品类别(手机、电视等).我正在考虑使用模板片段缓存,但在这个片段中,我将遍历这些类别的列表.这个列表在网站的任何部分都可用,所以它在我的 base.html 文件中.渲染模板时,我是否必须始终发送类别列表?或者有没有更动态的方法来做到这一点,使列表在模板中始终可用?

I'm trying to find a way to cache the results of a query that won't change with frequency. For example, categories of products from an e-commerce (cellphones, TV, etc). I'm thinking of using the template fragment caching, but in this fragment, I will iterate over a list of these categories. This list is avaliable in any part of the site, so it's in my base.html file. Do I have always to send the list of categories when rendering the templates? Or is there a more dynamic way to do this, making the list always available in the template?

推荐答案

将您的缓存查询放入 Django 的缓存:

Pop your cached query into Django's cache:

from django.core.cache import cache

cache.set('key', queryset)

然后创建一个上下文处理器,将缓存的值添加到所有模板中:

Then create a context processor to add the value of the cache to all templates:

# myproject/myapp/context_processors.py

from django.core.cache import cache

def cached_queries():
    return {'cache', cache.get('key')}

然后在 Django 设置文件中添加上下文处理器:

TEMPLATE_CONTEXT_PROCESSORS += (
    'myproject.myapp.context_processors.cached_queries'
)

现在,您将能够访问所有通用模板和所有具有请求上下文的模板中的 cache 变量,如果在视图中完成,则会提供模板:

Now you will be able to access the cache variable in all generic templates and all templates which have a requests context, which a template is given if this is done in the view:

return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))

何时设置缓存

这取决于缓存中包含的内容.然而,一个常见的问题是 Django 只有在发送页面请求时才真正执行 Python,而这通常不是您想要执行此类工作的地方.

When to Set the Cache

It depends on what is contained in the cache. However a common problem is that Django only really gets to execute Python whenever a page request is sent, and this is often not where you want to do this kind of work.

另一种方法是创建一个自定义管理命令对于特定的应用程序.然后,您可以在必要时手动运行它,或者更常见地将其设置为作为 cron 作业.

An alternative is to create a custom management command for a particular app. You can then either run this manually when necessary, or more commonly set this to run as a cron job.

要创建管理命令,您必须在位于应用程序内的 management/commands 目录中创建一个从 Command 继承的类:

To create a management command you must create a class decended from Command inside of a management/commands directory located inside of an app:

# myproject/myapp/management/commands/update_cache.py

from django.core.management.base import NoArgsCommand
from django.core.cache import cache

class Command(NoArgsCommand):
    help = 'Refreshes my cache'

    def handle_noargs(self, **options):
        cache.set('key', queryset)

此文件的名称很重要,因为这将是命令的名称.在这种情况下,您现在可以在命令行上调用它:

The name of this file is important as this will be the name of the command. In this case you can now call this on the command line:

python manage.py update_cache

这篇关于在 Django 中缓存查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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