2

I'm using a Redis cache (django-redis) for my Django app hosted on Heroku. To be more specific (though I don't think it's relevant to the probable solution), I'm using the Redis Cloud add-on.

How do I clear the cache upon deploy? I'm looking for an answer similar to Clear Memcached on Heroku Deploy, except for Django, not Rails.

Community
  • 1
  • 1
fangsterr
  • 3,670
  • 4
  • 37
  • 54

2 Answers2

6

Figured out how to make this work (combination of MagnusGraviti's answer and some help from heroku IRC).

Step 1:

Create a custom command to clear cache. See https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ or install django-clear-cache https://github.com/rdegges/django-clear-cache.

Step 2:

Create a script (for example, scripts/web) to place the command in [from the project root level]. For example, I prepended my Procfile web command with python manage.py clearcache && as below:

scripts/web

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

Step 3:

Then, you need to set the script to executable. On my OSX machine, the command was just:

chmod a+x scripts/web

Step 4:

Modify the Procfile to run script instead of a command:

web: scripts/web

That's it!

fangsterr
  • 3,670
  • 4
  • 37
  • 54
3

You have next options:

  • I always thought it was possible (and foreman locally worked for me with &&. Write your command python manage.py clear_cache and use it before launching server in Procfile:

web: python manage.py clear_cache && gunicorn...

  • If you use CircleCI you can edit your circle.yml file to clear cache after deployment

  • If you wrote fabfile you can include python manage.py clear_cache after deployment.

clear_cache command example:

`

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()
shalakhin
  • 4,586
  • 5
  • 25
  • 30
  • This answer seems promising, except I'm unable to get foreman to recognize two commands in one line (the "web: python manage.py clearcache && ..." solution). How do I account for this? – fangsterr Jul 07 '13 at 18:34
  • I just now made Procfile with `web: python manage.py clear_cache && gunicorn myapp.wsgi` and made `foreman start`. It works. What error foreman gives you? – shalakhin Jul 07 '13 at 18:46
  • mine's a little different. "web: python manage.py clearcache && newrelic-admin run-program gunicorn doorstep.wsgi -b 0.0.0.0:$PORT -w 5 --preload" gives me the error " manage.py: error: no such option: -b" – fangsterr Jul 07 '13 at 18:51
  • Does this set of commands launch without foreman successfully? – shalakhin Jul 07 '13 at 19:09
  • python manage.py clearcache works by itself. and the procfile without the "python manage.py clearcache && ..." worked – fangsterr Jul 07 '13 at 19:10
  • Oh, then it seems to be not an option to use &&. Try writing your own fabfile for deploy. It is not difficult to do and you'll be able to make database migration, clear cache etc. – shalakhin Jul 07 '13 at 19:53
  • figured out how to complete the puzzle from heroku irc. Posted my answer below – fangsterr Jul 07 '13 at 20:03