python清除缓存的命令_python – 重启django服务器时清除缓存的最佳位置
將代碼放在settings.py中,而不是賦值是不好的做法。它更適合作為管理命令:
from django.core.management.base import BaseCommand
from django.core.cache import cache
class Command(BaseCommand):
def handle(self, *args, **kwargs):
cache.clear()
self.stdout.write('Cleared cache\n')
您可以通過(guò)將其粘貼到someapp / management / commands中來(lái)添加到項(xiàng)目中。例如,您可以創(chuàng)建一個(gè)名為utils的新應(yīng)用程序,并將其添加到INSTALLED_APPS中,目錄結(jié)構(gòu)將如下所示:
utils
├── __init__.py
└── management
├── __init__.py
└── commands
├── __init__.py
└── clearcache.py
您現(xiàn)在可以通過(guò)執(zhí)行./manage.py clearcache來(lái)清除緩存。如果你想在每次運(yùn)行服務(wù)器時(shí)運(yùn)行clearcache,你可以寫(xiě)一個(gè)shell別名來(lái)做到這一點(diǎn):
alias runserver='./manage.py clearcache && ./manage.py runserver'
from django.conf import settings
# obviously change CACHES to your settings
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake'
}
}
settings.configure(CACHES=CACHES) # include any other settings you might need
from django.core.cache import cache
cache.clear()
編寫(xiě)這樣的獨(dú)立腳本將阻止循環(huán)導(dǎo)入,并允許您從settings.py導(dǎo)入它。雖然不能保證settings.py只能導(dǎo)入一次,但是一般來(lái)說(shuō),我會(huì)避免這樣做。如果信號(hào)框架每次啟動(dòng)應(yīng)用程序時(shí)都會(huì)觸發(fā)一個(gè)事件,那么在這樣的設(shè)置被加載之后,這將是很好的。
總結(jié)
以上是生活随笔為你收集整理的python清除缓存的命令_python – 重启django服务器时清除缓存的最佳位置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: windows mobile设置插移动卡
- 下一篇: 苹果错误分析报告preferreuser