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.

2 Answers 2

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!

1
  • 1
    Nice! :-) Congratulations!
    – shalakhin
    Jul 7, 2013 at 20:07
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()
7
  • 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 7, 2013 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 7, 2013 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 7, 2013 at 18:51
  • Does this set of commands launch without foreman successfully?
    – shalakhin
    Jul 7, 2013 at 19:09
  • python manage.py clearcache works by itself. and the procfile without the "python manage.py clearcache && ..." worked
    – fangsterr
    Jul 7, 2013 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.