【问题标题】:Cronjob to periodically refresh cache for django viewCronjob 定期刷新 django 视图的缓存
【发布时间】:2012-10-12 15:59:24
【问题描述】:

我在尝试为特定的 django 视图每小时重置一次缓存时遇到了一些麻烦。

现在,我正在使用 cache_page 装饰器通过 Memcached 缓存我的视图。但是缓存会在一段时间后过期,并且某些用户的请求未缓存。

@cache_page(3600)
def my_view(请求):
...

我如何编写我自己的 django manage.py 命令以每小时从 cron 刷新我的缓存以用于此视图?

换句话说,我在此处的答案中提到的 refresh_cache.py 文件中放入了什么: Django caching - can it be done pre-emptively?

谢谢!

【问题讨论】:

    标签: python django cron memcached


    【解决方案1】:

    我想扩展罗伯茨的答案以填写# Code to refresh cache

    Timezome+location 使缓存很难使用,就我而言,我只是禁用了它们,我也不确定在应用程序代码中使用测试方法,但它似乎工作得很好。

    from django.core.management.base import BaseCommand, CommandError
    from django.test.client import RequestFactory
    from django.conf import settings
    
    from ladder.models import Season
    from ladder.views import season as season_view
    
    
    class Command(BaseCommand):
        help = 'Refreshes cached pages'
    
        def handle(self, *args, **options):
            """
            Script that calls all season pages to refresh the cache on them if it has expired.
    
            Any time/date settings create postfixes on the caching key, for now the solution is to disable them.
            """
    
            if settings.USE_I18N:
                raise CommandError('USE_I18N in settings must be disabled.')
    
            if settings.USE_L10N:
                raise CommandError('USE_L10N in settings must be disabled.')
    
            if settings.USE_TZ:
                raise CommandError('USE_TZ in settings must be disabled.')
    
            self.factory = RequestFactory()
            seasons = Season.objects.all()
            for season in seasons:
                path = '/' + season.end_date.year.__str__() + '/round/' + season.season_round.__str__() + '/'
                # use the request factory to generate the correct url for the cache hash
                request = self.factory.get(path)
    
                season_view(request, season.end_date.year, season.season_round)
                self.stdout.write('Successfully refreshed: %s' % path)
    

    【讨论】:

      【解决方案2】:

      在您的应用程序中,您可以创建一个名为 management 的文件夹,其中包含另一个文件夹 commands 和一个空的 __init__.py 文件。在commands 中创建另一个__init__.py 和一个用于编写自定义命令的文件。我们称之为refresh.py

      # refresh.py
      
      from django.core.management.base import BaseCommand, CommandError
      from main.models import * # You may want to import your models in order to use  
                                # them in your cron job.
      
      class Command(BaseCommand):
          help = 'Posts popular threads'
      
          def handle(self, *args, **options):
          # Code to refresh cache
      

      现在您可以将此文件添加到您的 cron 作业中。你可以看看这个tutorial,但基本上你使用 crontab -e 来编辑你的 cron 作业和 crontab -l 来查看哪些 cron 作业正在运行。

      您可以在Django documentation 中找到所有这些以及更多信息。

      【讨论】:

      • 感谢罗伯特的回复。但是您能否告诉我“#code to refresh cache”中的内容。如何调用视图并使用 cache.set() 将响应设置为缓存
      • 你不调用视图。相反,您添加执行刷新的部分。这是因为视图可能正在执行您在 cron 作业中不希望执行的其他操作。具体见docs.djangoproject.com/en/dev/topics/cache/…
      • 如果我想缓存我的整个视图(这是我的主页)怎么办?如何将缓存设置为从 refresh.py 中的句柄函数响应整个视图?
      • 文档似乎表明 cache_page 不会缓存整个视图,即每个进程,而只是它的输出。如果这是真的,你可以摆脱这个输出调用 .delete()。为缓存视图添加前缀并探索正在使用的键,以便您确切知道要删除的内容。根据“删除缓存视图”命令,我不知道是否存在。
      • 嗯...这对你有帮助code.djangoproject.com/ticket/5815。其实有几种解决方案:stackoverflow.com/questions/2268417/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2011-01-17
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多