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

查看:918
本文介绍了在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设置文件

Then add your context processor in your Django settings file:

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

现在,您将可以访问所有通用模板中的缓存变量和具有请求上下文的所有模板,如果在视图中完成,则给出模板:

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.

要创建一个管理命令,您必须创建一个从 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天全站免登陆