best place to clear cache when restarting django server

后端 未结 5 1768
野的像风
野的像风 2020-12-13 00:45

I want memcached to be flushed on every restart/reload of django server. I use cherrypy for production and builtin server for development.

I would add this to settin

5条回答
  •  心在旅途
    2020-12-13 01:26

    If you have multiple cache backends, django.core.cache.cache.clear() will only clear the default cache. To make sure your clear_cache command clears cache from all your backends, you should use the following command:

    from django.conf import settings
    from django.core.cache import caches
    
    from django.core.management.base import BaseCommand
    
    class Command(BaseCommand):
        help = "Clear cache"
    
        def handle(self, **options):
            for k in settings.CACHES.keys():
                caches[k].clear()
                self.stdout.write("Cleared cache '{}'.\n".format(k))
    

提交回复
热议问题