【发布时间】:2013-07-04 19:23:15
【问题描述】:
我正在为 Heroku 上托管的 Django 应用程序使用 Redis 缓存 (django-redis)。更具体地说(尽管我认为这与可能的解决方案无关),我正在使用 Redis Cloud 插件。
如何在部署时清除缓存?我正在寻找类似于Clear Memcached on Heroku Deploy 的答案,除了 Django,而不是 Rails。
【问题讨论】:
我正在为 Heroku 上托管的 Django 应用程序使用 Redis 缓存 (django-redis)。更具体地说(尽管我认为这与可能的解决方案无关),我正在使用 Redis Cloud 插件。
如何在部署时清除缓存?我正在寻找类似于Clear Memcached on Heroku Deploy 的答案,除了 Django,而不是 Rails。
【问题讨论】:
想出了如何完成这项工作(结合 MagnusGraviti 的回答和 heroku IRC 的一些帮助)。
第 1 步:
创建自定义命令以清除缓存。请参阅https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ 或安装 django-clear-cache https://github.com/rdegges/django-clear-cache。
第 2 步:
创建一个脚本(例如,scripts/web)以将命令放置在 [从项目根级别] 中。例如,我在我的 Procfile web 命令前面添加了python manage.py clearcache &&,如下所示:
脚本/网络
python manage.py clearcache && gunicorn myapp.wsgi -b 0.0.0.0:$PORT -w 5 --preload
第 3 步:
然后,您需要将脚本设置为可执行。在我的 OSX 机器上,命令只是:
chmod a+x scripts/web
第 4 步:
修改 Procfile 以运行脚本而不是命令:
web: scripts/web
就是这样!
【讨论】:
你有下一个选择:
python manage.py clear_cache 并在 Procfile 中启动服务器之前使用它:
web: python manage.py clear_cache && gunicorn...
如果您使用 CircleCI,您可以编辑您的 circle.yml 文件以在部署后清除缓存
如果您写了fabfile,则可以在部署后包含python manage.py clear_cache。
clear_cache 命令示例:
`
from django.core.management.base import BaseCommand
from django.core.cache import cache
class Command(BaseCommand):
"""
Command to clear cache
"""
help = "Clear cache"
def handle(self, *args, **options):
cache.clear()
【讨论】:
web: python manage.py clear_cache && gunicorn myapp.wsgi 制作了Procfile 并制作了foreman start。有用。工头给你什么错误?