我想扩大对罗伯茨的答案填写# Code to refresh cache
Timezome +地理位置使缓存多少很难用,在我来说,我只是禁止他们的工作,也是我不知道如何使用的测试方法应用程序代码,但它似乎很好地工作。
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)
感谢您的回复Robert。但是,请您告诉我“#code刷新缓存”的含义。如何调用视图并使用cache.set()设置对缓存的响应 – Kiran
您不调用该视图。相反,您添加了进行刷新的部分。这是因为该视图可能正在做一些你不想在cron工作中做的事情。至于具体细节,请参阅https://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs#the-low-level-cache-api –
如果我想缓存我的整个视图(这是我的主页)?如何将refresh缓存设置为refresh.py中句柄函数的整个视图的响应? – Kiran