【问题标题】:Django Heroku Clear Cache on deploy部署时 Django Heroku 清除缓存
【发布时间】:2013-07-04 19:23:15
【问题描述】:

我正在为 Heroku 上托管的 Django 应用程序使用 Redis 缓存 (django-redis)。更具体地说(尽管我认为这与可能的解决方案无关),我正在使用 Redis Cloud 插件。

如何在部署时清除缓存?我正在寻找类似于Clear Memcached on Heroku Deploy 的答案,除了 Django,而不是 Rails。

【问题讨论】:

    标签: django heroku redis


    【解决方案1】:

    想出了如何完成这项工作(结合 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
    

    就是这样!

    【讨论】:

    • 不错! :-) 恭喜!
    【解决方案2】:

    你有下一个选择:

    • 我一直认为这是可能的(并且工头在本地使用 && 为我工作。编写您的命令 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 clearcache && ...”解决方案)。我该如何解释?
    • 我刚刚用web: python manage.py clear_cache && gunicorn myapp.wsgi 制作了Procfile 并制作了foreman start。有用。工头给你什么错误?
    • 我的有点不同。 “web:python manage.py clearcache && newrelic-admin run-program gunicorn doorstep.wsgi -b 0.0.0.0:$PORT -w 5 --preload”给了我错误“manage.py:错误:没有这样的选项:- b"
    • 这组命令在没有工头的情况下启动成功吗?
    • python manage.py clearcache 自己工作。并且没有“python manage.py clearcache && ...”的procfile工作
    猜你喜欢
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多