部署时的Django Heroku Clear Cache

Django Heroku Clear Cache on deploy

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

部署后如何清除缓存? 我正在寻找类似于Heroku Deploy上的Clear Memcached的答案,除了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 &&,如下所示:

脚本/网络

1
python manage.py clearcache && gunicorn myapp.wsgi -b 0.0.0.0:$PORT -w 5 --preload

第三步:

然后,您需要将脚本设置为可执行文件。在我的OSX机器上,命令只是:

1
chmod a+x scripts/web

第4步:

修改Procfile以运行脚本而不是命令:

1
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命令示例:

`

1
2
3
4
5
6
7
8
9
10
11
12
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()